Failed to load viewstate. Happening only occasionally. Tough to recreate
Asked Answered
P

9

16

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)
Punjab answered 24/2, 2009 at 6:47 Comment(3)
Are you running this in a web farm? Where the web app is installed on more than one web server? I've heard the machine key must be the same within IIS's metabase if so.Achlamydeous
Very true, however the error would present it self as "Failure to decrypt ViewState" ... something along those lines...Pereira
This error is very hard to be reproduced... as its not at all happening on my local computer but happening on the server (Windows Server 2003)Punjab
D
24

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.

Disaccord answered 24/2, 2009 at 7:55 Comment(3)
Might help someone: How to Disable “Just My Code” in Visual Studio 2005 johnlamansky.com/blog/…Punjab
Why was I never ever able to find how to do this?! Thank you so much for this!Rad
And this is for vs 2013:Kicksorter
N
13

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.

Nevarez answered 24/2, 2009 at 7:43 Comment(1)
This is very common with data bound controls when the underlying data had changed.Coumarin
F
5

There is a workaround for this error:

Set EnableViewstate property of all dynamically loading controls to false. This is not a complete solution; we are disabling controls' ViewState here to bypass this error.

This solution can cause other errors to raise their heads, so be careful when applying this fix.

Ferguson answered 10/2, 2010 at 9:38 Comment(1)
This might feel kludgy, but it is useful in situations where you know the ViewState doesn't apply, such as when you are redirecting the browser to a new page on a PostBack.Platitudinize
P
4

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.

Pereira answered 24/2, 2009 at 6:50 Comment(0)
B
3

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

Befit answered 23/8, 2010 at 20:41 Comment(2)
Exactly 100%. This fixed our issue.Manzano
dynamic controls in viewstate for the win!Blondy
M
2

This should probably do it for you.

http://yakshaver.blogspot.com/2011/03/greatest-viewstate-attribute-to-ever.html

Mccune answered 27/3, 2011 at 14:11 Comment(0)
K
0

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.

Kory answered 24/2, 2009 at 19:25 Comment(0)
L
0

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.

Levasseur answered 13/8, 2009 at 4:32 Comment(0)
A
0

I experienced the exact same problem. I had 2 listview in a Master page, associated with 2 updatedpanels.

Disabling the EnableViewState on the Listviews solved it for me!

Annalee answered 7/2, 2011 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.