The best fix to this problem is to avoid using binary serialization. Binary serialization uses many resources even when you do not run into this problem. Instead, limit what you put in view state to a combination of Arrays, Pairs, Triplets, and simple types (for example, strings, int, and other types).
Invalid viewstate can happen for a variety of reasons. Viewstate is too big and has not finished rendering before a user causes a postback on the page.
View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.
View state is used automatically by the ASP.NET page framework to persist information that must be preserved between postbacks. This information includes any non-default values of controls. You can also use view state to store application data that is specific to a page.
Well it depends. Invalid viewstate can happen for a variety of reasons.
Whatever you do do not turn off viewstate or event validation.
One issue can be to do with users routers truncating form fields. The way around this is to set the MaxPageStateFieldLength to a smallish number (like 100) in web.config and the ViewState gets broken up into small chunks. It's very simple to do, and this article explains it fully.
Exceptions don't "just happen" from time to time. They always occur for valid reasons, some of which are already listed in the other answers.
However, to alleviate problems with ViewState consider disabling it altogether. As ASP.NET developers we often tend to use ViewState in all sort of places where it isn't needed because its the default. I usually think about using static html before I consider using a control. If you do decide to use a control think about if it really needs ViewState to be enabled. Disabling it often lead to better page load times, so if you can, do it.
I wish it were disabled by default so people were forced to think this way, but it isn't.
Update to answer comment:
Of the top of my head I come up with 3 opportunities to turn off ViewState.
Disable ViewState if data is loaded on every postback. This will often be the case if you're building AJAX enabled sites (that's real AJAX not that UpdatePanel kind ;) ), where you usually load data on the first load and then reload/update data using AJAX requests. In some cases you might even load data on every visit for the sole purpose of disabling ViewState, and then cache the data on the server instead.
You can also consider disabling ViewState if you databind to content which is really static. Sometimes I find a list which is databound to a small static basedata table in the database or something like that. Now, this can be dangerous, but if I'm convinced that the data wont change I might move the data into the page as static content (you could wrap it in a separate control so you won't have several static copies of the data). But if the data then DO change, you will have to change it manually.
Simple controls such as Labels are often good candidates for disabling ViewState.
Finally you could switch to ASP.NET MVC framework and wave goodbye to these problems forever, that's what I'm planning to do even if I will face some other problems. ;)
BlowDart has the right answer for the Invalid Viewstate problem. It's probably your app pool being recycled and changing the encryption key.
See these posts for support:
Erratic Invalid Viewstate issue in a .NET application
Making user login persistant with ASP .Net Membership
invalid view state don't have any value for your logger or for users or for your website,End users never sees those errors. to avoid this error try to add the following In Global.ascx:
void Application_Error(object sender, EventArgs e)
{
if (ex is HttpException && ex.InnerException is ViewStateException)
{
Response.Redirect(Request.Url.AbsoluteUri);
return;
}
}
for more info check the following link:
https://www.karpach.com/viewstateexception-invalid-viewstate.htm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With