<p>I'm running some c# .net pages with various gridviews. If I ever leave any of them alone in a web browser for an extended period of time (usually overnight), I get the following error when I click any element on the page.</p>

<p>I'm not really sure where to start dealing with the problem. I don't mind resetting the page if it's viewstate has expired, but throwing an error is unacceptable!</p>

<pre><code>  Error: The state information is invalid for this page and might be corrupted.

  Target: Void ThrowError(System.Exception, System.String, System.String, Boolean)

  Data: System.Collections.ListDictionaryInternal

  Inner: System.Web.UI.ViewStateException: Invalid viewstate. Client IP: 66.35.180.246 Port: 1799 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0 ViewState: (**Very long Gibberish Omitted!**)

  Offending URL: (**Omitted**)

  Source: System.Web

  Message: The state information is invalid for this page and might be corrupted.

  Stack trace: at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) at System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded() at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) at System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
</code></pre>

[{'answer_id': 73402, 'author': 'MrHinsh - Martin Hinshelwood', 'author_id': 11799, 'author_profile': 'https://Stackoverflow.com/users/11799', 'pm_score': 1, 'selected': False, 'text': '<p>You can remove this error completely by saving your view state to a database and only cleaning within the duration you need to. This also sygnificantly improves the performance of your pages even shen using relatively small viewstates.</p>\n\n<p>At the very least you can inherit from the Page class and add your own ViewStateLoad routen that check to see if it has expired and reloads the default state.</p>\n\n<p>Check <a href="http://www.codeproject.com/KB/viewstate/ViewStateProvider.aspx" rel="nofollow noreferrer">ViewState Provider - an implementation using Provider Model Design Pattern</a> for providing a custom Viewstate provider.</p>\n'}, {'answer_id': 73450, 'author': 'Toby Mills', 'author_id': 12377, 'author_profile': 'https://Stackoverflow.com/users/12377', 'pm_score': 0, 'selected': False, 'text': '<p>Alternatively if you know the time-out length then you could add a bit of javascript to the page which redirects the user to an alternative page if there has been no activity on the page after a preset period of time. You can then extend this to warn the customer that their session / page is about to expire and provide them with a means to extend it (e.g. javascript server call back).</p>\n'}, {'answer_id': 73463, 'author': 'Gilligan', 'author_id': 12356, 'author_profile': 'https://Stackoverflow.com/users/12356', 'pm_score': 2, 'selected': False, 'text': '<p>That is odd as the ViewState is stored as a string in the webpage itself. So I do not see how an extended period of time would cause that error. Perhaps one or more objects on the page have been garbage collected or the application reset, so the viewstate is referencing   old controls instead of the controls created when the application restarted. </p>\n\n<p>Whatever the case, I feel your pain, these errors are never pleasant to debug, and I have no easy answer as to how to find the problem other than perhaps studying <a href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx" rel="nofollow noreferrer">how ViewState works</a></p>\n'}, {'answer_id': 73757, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>The above posts give you some answers on solving the problem.  If just handling the ugly error in the interim is what you\'re looking for, custom errors are the easiest way to gracefully handle all your "ugly yellow errors"</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/aa479319.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa479319.aspx</a></p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx</a></p>\n'}, {'answer_id': 77377, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 0, 'selected': False, 'text': '<p>Another option is to add in a global error handler, that would capture the exception at the application level and redirect the user to a "Session Elapsed" page. </p>\n\n<p>If you want an idea of a general implementation of a global error handler, I have one available on my website, I can give you the code if needed - <a href="http://iowacomputergurus.com/free-products/asp.net-global-error-handler.aspx" rel="nofollow noreferrer">http://iowacomputergurus.com/free-products/asp.net-global-error-handler.aspx</a> </p>\n'}]
['https://Stackoverflow.com/questions/73380', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12382/']