views:

5493

answers:

9

Details of Error are given below. This error happens only occasionally / rarely / sometimes and there aren't any steps to reproduce it.

How can I know which Control is throwing this Viewstate error???

Error Message: 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.

When: Rarely / Occasionally

Steps to Recreate: Can't

Stack Trace: System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Web.HttpException: 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. at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) at System.Web.UI.Control.LoadViewStateRecursive(Object savedState) at System.Web.UI.Page.LoadAllState() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.sessions_aspx.ProcessRequest(HttpContext context) in c:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\73694782\85423fe4\App_Web_mnrmfsfa.8.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

+2  A: 

I've experienced this error, it happens when there is a cross page postback.

For example: You load View A. View A loads fine, for whatever reason the conditions under which View A loaded, no longer exist. The fallback page is View B. So the user completes the form on View A and postsback. Since View A's conditions are no longer met, View A's form values are posted to View B.

Chuck Conway
+2  A: 

More generally this error happens when the control hierarchy of the page changes in a way that prevents the framework to load the view state. The view state mechanism assumes that the control hierarchy is the same on load as it was when it was saved.

This might look as a random error because there are cases when changing the controls in the page does not prevent the view state from loading. Have a look in your page and look for controls that are dynamically created/deleted. Ensure that any controls are added to the page before the view state is loaded (that would be before page Load event).

Unfortunately there is no way to see which exactly is the guilty control that is not added correctly to the control hierarchy. A common way to see this error is to add some dynamic controls on an event (for example on a drop down selected index changed) - this way their state is saved to view state - but not add them again on postback - this way the view state is invalid because the controls do not exists any more when the view state is loaded.

Aleris
This is very common with data bound controls when the underlying data had changed.
Jason Hernandez
+8  A: 

Unfortunately there is no way to see which exactly is the guilty control that is not added correctly to the control hierarchy.

There is!

Disable "Just my code" in debugging settings. And catch all thrown exceptions (check 'thrown' for Common Language Runtime in the dialog opened by Ctrl-Alt-E).

After the exception occurs, go to the nearest stack frame where the Control object is available and examine its ClientID and parents.

amartynov
Might help someone:How to Disable “Just My Code” in Visual Studio 2005http://www.johnlamansky.com/blog/how-to-disable-just-my-code-in-visual-studio-2005/
A: 

I've seen this error when using a gridview. It would happen when user interaction caused a postback before the page was done rendering completely.

I believe it was fixed in VS2005 SP1 however.

Daniel Auger
A: 

i remember getting this when i first started ajax and there were just red herring posts about it all over the internet but noone had a clue why it was happening. I didn't touch ajx for ages as i just hate updatepanels etc they are so lame and i never actually resolved this the first time.

but anyway...here i am 3 years later experiencing the same thing again but thankfully there is much more info on this subject.

doesn't help too much now as i've got so many controls on the page it really could be any of them...

anyway, thanks for the article. i think i'm headed in the right direction.

A: 

You can probably reproduce this by stopping the page before it is fully loaded, and then submitting a postback. If the server wasn't able to get the complete viewstate from the server, then it won't be able to re-create it on postback, hence the crash.

Noon Silk
+2  A: 

There is a workaround for this error, Set EnableViewstate property of all dynamically loading controls to false. This is no a complete solution. we are disableing controls viewstate here to bypass this error. This solutions can cause other errors to raise their heads.

so be careful before applying this fix.

theLazyCoder;

Raje
+1  A: 

There is a workaround for this error, Set EnableViewstate property of all dynamically loading controls to false. This is no a complete solution. we are disableing controls viewstate here to bypass this error. This solutions can cause other errors to raise their heads.

so be careful before applying this fix.

theLazyCoder;

This worked well where I am using Ajax and the viewstate is not important. Thanks!

A: 

I had this error when my custom controls were accidentally created in Page_Load(). Controls should be dynamically created in OnInit().

MsBugKiller