This is a migrated thread and some comments may be shown as answers.

AJAX and Dynamic Controls

3 Answers 137 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Michael Landy
Top achievements
Rank 1
Michael Landy asked on 08 Mar 2010, 05:55 PM
I am trying to understand what is happening and I need a lifecycle explanation. 

I have an RadAjaxPanel where on page load, I dynamically creat and populate three comboboxes.  Combobox 1 drives the data in combobox 2 and combobox 3.  I have set the autopostback to true on these controls.  Combobox 1 defaults to Item A, Combobox 2 defaults to Item M, Combobox 3 defaults to Item X.

If I change Combobox 1 to Item B, this change is not see during the page load event because the control is being created.  Therefore, combobox 2 and combobox 3 are filled with the items that correspond to Item A in combobox 1.  Since "viewstate" has not been applied at this time, the combobox 1 thinks its value is Item A.

During the SelectedItem event, the viewstate has loaded and the combobox now has item B.  I assume that at this point I can clear out and re-load combobox 2 and 3 to have their data on the page updated.

Just for fun, I decided to fill combobox 2 with different data on the page load event if the page was in a postback.  Imagine my surprise when the combobox 2 showed the original data that was loaded the first time in the page and not the "New" data I forced in during the postback.  Why is this?  Is the viewstate holding all of the valid data items for this control and resetting it after the page load event?




3 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 09 Mar 2010, 11:36 AM
Hello Michael,

Can you share the problematic page code with us so we can investigate the issue further and try finding a proper resolution for you?

Looking forward your reply,
Iana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Michael Landy
Top achievements
Rank 1
answered on 10 Mar 2010, 01:15 AM

Here is some sample code.  The biggest item of concern to me is that during a postback, I do not reload the data in the dynamic controls, however, the data is there when the page is refreshed.  This is not so much a problem as I need to understand why dynamic controls do not need to have their data re-loaded on a postback event.

Here is the code:

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using Telerik.Web.UI;  
 
    public partial class Trials : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            RadComboBox cmb1 = new RadComboBox();  
            cmb1.ID = "first";  
            cmb1.AutoPostBack = true;  
            cmb1.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(cmb1_SelectedIndexChanged);  
 
            updatePanel.Controls.Add(cmb1);  
 
            RadComboBox cmb2 = new RadComboBox();  
            cmb2.ID = "second";  
            cmb2.AutoPostBack = true;  
            cmb2.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(cmb1_SelectedIndexChanged);  
 
            updatePanel.Controls.Add(cmb2);  
 
            RadComboBox cmb3 = new RadComboBox();  
            cmb3.ID = "third";  
            cmb3.AutoPostBack = true;  
            cmb3.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(cmb1_SelectedIndexChanged);  
 
            updatePanel.Controls.Add(cmb3);  
 
            if (!this.IsPostBack)  
            {  
 
                cmb1.Items.Add(new RadComboBoxItem("Item A""1"));  
                cmb1.Items.Add(new RadComboBoxItem("Item B""2"));  
                cmb1.Items.Add(new RadComboBoxItem("Item C""3"));  
 
                cmb2.Items.Add(new RadComboBoxItem("Item M""1"));  
                cmb2.Items.Add(new RadComboBoxItem("Item N""2"));  
                cmb2.Items.Add(new RadComboBoxItem("Item O""3"));  
 
                cmb3.Items.Add(new RadComboBoxItem("Item X""1"));  
                cmb3.Items.Add(new RadComboBoxItem("Item Y""2"));  
                cmb3.Items.Add(new RadComboBoxItem("Item Z""3"));  
            }  
 
        }  
 
        void cmb1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)  
        {  
            string sSelect = e.Text;  
            string sValue = e.Value;  
 
            RadComboBox dd = (RadComboBox)o;  
 
            if (dd.ID == "first")  
            {  
                RadComboBox cb = updatePanel.FindControl("second"as RadComboBox;  
                if (cb != null)  
                {  
                    cb.Items.Clear();  
                    cb.Items.Add(new RadComboBoxItem("Item XXX""14"));  
                    cb.Items.Add(new RadComboBoxItem("Item YYYY""32"));  
                    cb.Items.Add(new RadComboBoxItem("Item ZZZ""23"));  
 
                }  
            }  
 
 
        }  
    }  
 
0
Iana Tsolova
Telerik team
answered on 10 Mar 2010, 02:55 PM
Hi Michael,

RadComboBox keeps its state in the ViewState and that is why through postbacks its items collection is retained. On the opposite, if you disable the controls or the page ViewState you will see that the comboboxes will be empty after a postback if you explicitly do not fill/bind them.

I hope this helps.

Sincerely yours,
Iana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Ajax
Asked by
Michael Landy
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Michael Landy
Top achievements
Rank 1
Share this question
or