PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Failed to load viewstate Error

Posted by PANVEGA on August 27, 2009

When I developed a Custom Webpart I had to build some dynamic added ASP Control (DropDownList etc.) during and after postback. Then I received the follwing error message during the Webpart was changing the Controls.

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.

Reason:

For example, if I dynamically add a textbox to a page, that textbox MUST exist before the viewstate for the page is loaded after a postback. This is because the when the page attempts to load viewstate, the control hierarchy must match the control hierarchy that existed when viewstate was originally created.

A Viewstate restores values based on the IDs of contorls during postback. In order for this to work, ViewState expects the contorl tree to be the same before and after. The error basically tells you that the control tree is got on postback wasn’t the same as before.

Since you are using dynamic controls, make sure to give them an explicit ID, and to make sure htey are recreated on postback with those same IDs.

So when you add a control dynamically, it must exist during the postback – at least long enough for viewstate to be loaded properly. This usually involves re-adding any dynamic controls that were present at last page load. What this means is that you can dynamically add, update and remove controls, but only at certain times in the page’s lifecycle. Here is a breakdown of a typical page lifecycle:

  • Instantiate – by constructor.
  • Initialize – This is when the markup code is initialized into the control hierarchy. The Init event is fired and OnInit is called.
  • Load Viewstate – The LoadViewState method is called.
  • Load – The Load event is fired and the OnLoad method is called.
  • Pre Render – The PreRender event is fired and the OnPreRender method is called.
  • Save Viewstate – The SaveViewState method is called
  • Render – The Render method is called.
  • Unload – The Unload event is fired and the OnUnload method is called.

Solution:

I tried a view hours to change the ordering of the controls, however I still reveived the error above. So I decided to disable the ViewState for this particular Webpart when the page loads.

protected override void OnLoad(EventArgs e)
{
this.EnableViewState = false;
}

That´s it. Afterwards everything worked fine, because I did not need the ViewState in my Webpart.

Note: Do not disabling the viewstate for the entire page. Some functions in SharePoint does not work anymore.

Page Life cycle:

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important for you to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you intend.

Additionally, if you develop custom controls, you must be familiar with the page life cycle in order to correctly initialize controls, populate control properties with view-state data, and run any control behavior code.


http://www.15seconds.com/issue/020102.htm

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.webpart.loadviewstate.aspx

http://msdn.microsoft.com/en-us/library/ms178472.aspx?wt.slv=ColumnA

http://weblogs.asp.net/owscott/archive/2004/11/02/What-causes-ViewState-Errors.aspx

http://www.ironspeed.com/articles/Disable%20View%20State%20for%20a%20Page/Article.aspx

http://weblogs.asp.net/alessandro/archive/2008/01/04/failed-to-load-viewstate-typical-problem-with-an-obvious-solution.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: