Failed to load viewstate. Happening only occasionally. Tough to recreate

Roberto Carlos picture Roberto Carlos · Feb 24, 2009 · Viewed 36.6k times · Source

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)

Answer

amartynov picture amartynov · Feb 24, 2009

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.