C# – ‘System.Web.HttpException: Failed to load viewstate

asp.netcviewstate

I have a usercontrol which contains textbox's, dropdownlist controls, checkbox's, gridview, hyperlinks. some of the controls are displayed when the pageloads
while the rest of them are visible based on the selection by the user's from the dropdownlist controls and checkbox's.

The function of this page is the same the data entered by the user using the above controls into a file and display this file in a gridview along with
the user info like his name, date of creation, service type, contact info, time of service and comments along with the above created file.

The page works for me without any error but the users are getting 'System.Web.HttpException: Failed to load viewstate.' error occasionally.

How can i reproduce this error? How can I fix it?

Here is the entire error message:

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.Page.LoadAllState()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)       
   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.app_uatserviceoutcome_aspx.ProcessRequest(HttpContext context) 
   in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\bad754dd\a11f74ff\App_Web_uatserviceoutcome.aspx.ae7ca9bd.xvr6rpyt.0.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Any help is greatly appreciated.

Best Answer

This is a classic webforms gotcha that is annoyingly hard to track down - see this question for some suggestions