Failed to Load ViewState?

R

rockdale

Sometime my ASP.NET application got this error message but not always
everytime.

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


I am using PageTemplate and dynamic load my user defined control.

Basically when user click on a linkbutton I will point back to
default.aspx with a parameter fID. then based on the fID to load ascx
into default.aspx page. Sometimes I will get the Failed to Load
ViewState error but not every time. Source code is attached, any help
please?

private void Page_Load(object sender, System.EventArgs e)
{
String userCtrlName = "Home";
System.Web.UI.Control userCtrl;
String id="";

pbdTemplate.TemplatePath = "layoutTemplate.ascx";



if(Request.Params["fid"]!=null){
id = Request.Params["fid"].ToString ();

SqlStorage ss = new SqlStorage();
userCtrlName = ss.GetFunctionControl(Convert.ToInt32(id));
}

userCtrl = Page.LoadControl(userCtrlName);
content.Controls.Add(userCtrl);
}


I read some posts say disable viewstate, but this will disable most
events also (e.g. datagrid sorting...)

Thanks
-Rockdale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top