views:

1105

answers:

5

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.

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!


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)

+1  A: 

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.

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.

Check ViewState Provider - an implementation using Provider Model Design Pattern for providing a custom Viewstate provider.

MrHinsh
A: 

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).

Toby Mills
+2  A: 

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.

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 how ViewState works

Gilligan
A: 

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"

http://msdn.microsoft.com/en-us/library/aa479319.aspx

http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx

A: 

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.

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 - http://iowacomputergurus.com/free-products/asp.net-global-error-handler.aspx

Mitchel Sellers