I'm trying to load user controls dynamically.
When I load a user control first time it works fine but when I load another cotrol to replace my current control it gives an error.
Here is my main page:
private void Page_Load(object sender, System.EventArgs e){
control_id=ViewState["selected_category"].ToString();
if(control_id=="phones"
{
plhMain.Controls.Clear();
Control control = LoadControl ("ucPhones.ascx"
;
plhMain.Controls.Add(control);}
else if(control_id=="main"
{
plhMain.Controls.Clear();
Control control = LoadControl ("ucMain.ascx"
;
plhMain.Controls.Add(control);}
}
When I load User Control second time I receive the following error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
How Can I solve this problem?
Thanks in advance!
When I load a user control first time it works fine but when I load another cotrol to replace my current control it gives an error.
Here is my main page:
private void Page_Load(object sender, System.EventArgs e){
control_id=ViewState["selected_category"].ToString();
if(control_id=="phones"
plhMain.Controls.Clear();
Control control = LoadControl ("ucPhones.ascx"
plhMain.Controls.Add(control);}
else if(control_id=="main"
plhMain.Controls.Clear();
Control control = LoadControl ("ucMain.ascx"
plhMain.Controls.Add(control);}
}
When I load User Control second time I receive the following error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
How Can I solve this problem?
Thanks in advance!