Validation of viewstate mac failed - but only on occasion - asp.net

I am getting validation of view state MAC failing but only very very occasionally.
I am not in a web farm and can't recreate this.
Are there any known factors that can make view state MAC validation fail?

Microsoft blogger Tess Ferrandez has a pretty good post on this:
Viewstate and viewstate validation use a couple of hidden form fields
like __VIEWSTATE and __EVENTVALIDATION. If the page renders so slowly
that the __EVENTVALIDATION field has not rendered by the time someone
clicks the button or control that causes the postback, ASP.NET will
also believe that the viewstate is invalid and report this.
Check whether your viewstate is very large in the problematic page(s). You may want to turn off EnableViewState property on controls that don't need it, especially large databound controls that don't need to remember their state between postbacks.

Related

How to reduce viewstate size of server control?

I have developed a server control which inherits from CompositeDataBoundControl and INamingContainer. It has 2 TextBoxes and 1 small image.
But when rendering in GridView its ViewState size is up to 500 bytes. It also has about 20 properties, but I don't use ViewState here.
I only use get;set; and I bind it again when postback.
Now I am wondering how to reduce this ViewState size and how I can examine this ViewState . If I can examine the values of ViewState , it would be helpful to fix the problem.
Paste the ViewState into a base-64 decoder. I always find it enlightening to see what is taking up the space.
Keep in mind that the ViewState field also includes ControlState.
It has got 2 TextBoxs and 1 small image.
Some controls can function quite well without ViewState, due to their constant presence in the HTTP request. A TextBox is a good candidate for explicitly disabling ViewState because its value is always posted back to the server as part of the request.
An image is also a good candidate for disabling ViewState, unless calculating the URL requires processing time (e.g. a database call versus static assignment).
But when rendering in GridView...
A GridView almost definitely adds to the ViewState itself. Are you accounting for that?
you could disable the viewstate with
this.EnableViewState = false;
you could also check this link Disable View State for a Page to disable a viewstate from a page.
If you wish to check the viewstate, you could use ASP.NET ViewState Helper or a tool such as Fiddler to analyze the viewstate.

Large viewstate in HTML source

This is 10KB in my HTML source:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTEyOTAwODE4Nw9kFgJmD2QWAgIDD2QWBGYPZBYEAgEPDxYCHgdWaXNpYmxlaGRkAgIPZBYIAgEPFgIfAGdkAgMPDxYEHgRUZXh0BQNUb20eC05hdmlnYXRlVXJsB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CI+MjAgQXByaWwgMjAxMSBhdCAwMDowMTo0NzwvYWJicj5kAgMPDxYGHwIFC34vdXNlcnMvVG9tHwUFHlZpc2l0IHRoaXMgZ2FtZSBtYWtlcnMgcHJvZmlsZR8BBQNUb21kZAIFDw8WBB8CBQt+L3VzZXJzL1RvbR8FBSBUb20gbWFrZXMgZ2FtZXMgd2l0aCBDb25zdHJ1Y3QgMmQWAmYPDxYEHwgFSWh0dHA6Ly93d3cuZ3JhdmF0YXIuY29tL2F2YXRhci81MjcxZDMyODNkYjk1N2VmM2E4Njc2MWVkMTVjMTY5Nj9yPXBnJnM9ODAfBQUOVG9tJ3MgR3JhdmF0YXJkZAIHDw8WBh8GBRBzIGNvbW1lbnQtcmVwb3J0HwUFEFJlcG9ydCB0aGlzIHBvc3QfBwICZGQCCQ8PFgYfBgUQcyBjb21tZW50LWRlbGV0ZR8FBRBEZWxldGUgdGhpcyBwb3N0HwcCAmRkAgsPDxYGHwYFDnMgY29tbWVudC1lZGl0HwUFDkVkaXQgdGhpcyBwb3N0HwcCAmRkAg0PDxYGHwYFD3MgY29tbWVudC1xdW90ZR8FBQ9RdW90ZSB0aGlzIHBvc3QfBwICFgIfAwUFI1Bvc3RkAg8PFgIfAQUgcGlsbHV1w7rDuiB0dCB0IGggdGggdGggdGggdGggdGhkAgYPZBYCAgEPDxYCHglNYXhMZW5ndGgC9ANkZAIIDw8WAh8AaGRkZAx0/0RvMYX4SKMMjV5pXc+cjNqDEEepI0JWzIxpxOzG" />
This represents ~50% of the entire size of the page.
Why does it do this, why so long? Can I do anything about it? It's bad for mobile users.
What is this view state anyway and how to mitigate its size
In Asp.net WebForms every control saves its state because HTTP protocol is stateless and Asp.net WebForms pages bypass that by saving every control's state in this Base 64 encoded string. This is the only way for Asp.net framework to know whether some control's value has changed or not. But... This automatically means that static controls that don't get POSTed back to server (like label for instance) don't need to save their state. You can always set their EnableViewState="false".
Unfortunately this can't be set without any other code changes on other controls, that do get POSTed back (every server-side control that renders some sort of an input in HTML). This basically means that setting EnableViewState="false" on page level (within #Page directive) will have consequences that are seen as controls loosing their values, controls not firing certain events etc.
So, the more server-side controls you have the larger it will get (without turning it off on certain controls).
But I wouldn't worry if its size is 10k. That will go back and forth rather fast and painless. You will have problems when it gets much larger. I once worked on a project and we had an issue with a certain page (done by less experienced developer) where view state grew over 1MB. Imagine that. What a slowdown!
How to turn it off completely on page level
When you turn view state off on page level you have to be aware that certain controls that were loaded (or better said data bound) in on of your page's events, will have to be reloaded each time your page gets POSTed back at server. Otherwise they will show up as empty when your page gets back to the client.
Your server controls are filling the ViewState with data they will need on postback. If your page does not postback you can just disable the ViewState for the page.
To disable ViewState for the page you can just add EnableViewState="false" to the #Page directive. Please be aware you should only use this as a solution if you are 100% sure the page does not postback.
You also might want to check this MSDN article to get a better idea of what the ViewState does.
Disable viewstate for static controls, like a gridview.
Check out this question for more info:
If you are concerned about the viewstate on the client side, then think about storing it on the server side. Perhaps in a session variable. Take a look at this article as there is statistical comparison given. Download the solution and check out how to store it on the server side.
An Analysis of Keeping ViewState out of the Page
This article explained it neatly to me in the past: Taking a Bite Out of ASP.NET ViewState.
Basically viewstate's on by default and, depending on which controls you use, it can get out of hand pretty fast. Especially data controls like the gridview are responsible for massive injection of viewstate. You can disable that on a per control basis by setting the EnableViewState property to false. Be careful however as taking out viewstate might also take out functionality of the controls. So do it one by one and test test test.
Another way, and likely better for mobile, is to make use of ASP.NET MVC instead which doesn't have to deal with automatic viewstate injection.

How to profile use of Viewstate by controls

I have a couple of pages that are bulky due to the viewstate. I have following question:
is there any tool that can track viewstate of individual control on page and tell me which control is taking maxm viewstate
also can i know which controls viewstate is not being used and disable it?
There are some Viewstate Utilities listed here http://blogs.msdn.com/rextang/archive/2007/05/25/2868250.aspx. I always store Viewstate in the database rather than send it back and forth over the internet. Example Code here http://www.componentworkshop.com/blog/2009/06/27/advanced-net-storing-viewstate-in-a-database
You can turn on trace on the page and that should show how much viewstate is used for each control.
I have no good answer to your second question, but one rule of thumb I use when i develop webforms is that i set EnableViewState=false immediately after I've created an Ascx.
That way I can turn it on when I actually need it and not wonder if my databound controls uses alot of viewstate.

ViewState is taking 20% of my pages even though it's disabled!

I disabled viewstate in the web.config file (and there's no EnableViewState = true anywhere on the pages), but despite this, the pages are rendered with a quite large view state (8k for a 40k page). I checked the viewstate contents with a viewstate decoder and discovered that the multiview controls I'm using on my pages are the guilty ones. Is there anyway to make the multiview controls stop using the viewstate?
I'm thinking about creating a control class that inherits from MultiView and override the LoadViewState and SaveViewState methods but I'm leaving this as a last resort, any suggestions?
Thanks
here is a wonderful way to just get rid of viewstate from being sent over wire for each post-back. basically, it stores the complete viewstate as a session variable on the server and only transfers the identifier in the viewstate field.
compression will save you little bit in terms of bandwidth whereas putting getting viewstate out of the page will have quite dramatic performance improvement
the following articles explains several techniques with performance measurement metrics as well eggheadcafe
Since ASP.NET 2.0 the internal content of the ViewState hidden field is made up of the "old" ViewState (the ViewState state bag / dictionary) AND the ControlState. The Control State unlike the ViewState cannot be disabled and it is intended for the minimal information that a Control needs to function properly.
You cannot disable the ControlState and you either live with it either use a different (kind) of control on your page.
You could override the render for your page (or base page) scan for the viewstate hidden input and remove it from the writer.
steps:
do the base.render
output the htmlwriter contents to a string
remove input with __viewstate
write the new string to the HTMLWriter.
To answer my own question, I managed to get rid of the viewstate by removing the form runat="server" I had in my master page, now I only enclose the controls that really need postback in a form tag with runat=server. It seems to be discarding the control state as well (which is what I want, the page doesn't post back), will still have to investigate more though.
The only problem that's left is that when I add a form runat=server tag anywhere on the page, the Multiview finds my form tag and add its trash in the hidden viewstate field, I was thinking this would happen only if the multiview is enclosed in a form runat="server" tag but it's smart enough (or dumb enough in this regard) to find the form tag anyway.
The System.Web.UI.Page class has a property called PageStatePersister that you can override in your pages. Here you can return a PageStatePersister type object that overrides the default persistence mode for the pages viewstate.
As Vikram suggested you can use a SessionPageStatePersister to store viewstate in session instead of a hidden field. But you can also implement your own PageStatePersister that stores the viewstate in the Cache or a database or a file. Whatever you need really.
The thing you shouldn't do is to use the PageStatePersister to discard viewstate, for the viewstate is needed by some of your controls.

viewstate failing on postback

We have a web content management system (based on Sharepoint 2007/MOSS, but for the purposes of this problem, that is not relevant, so please stick around even if you haven't worked on MOSS!). On a given page, there are conditions we cannot change:
An editor clicks "Edit" and the page posts back.
When it reloads in edit mode, the control tree is entirely different.
ViewState must be enabled in edit mode, since the edit controls post back frequently
If we disable ViewState in presentation mode, everything works fine. ViewState gets set to "enabled" on the edit postback, the ViewState tree is built up for the first time as the edit controls are generated, and all is well.
If we enable ViewState in presentation mode, when transitioning from presentation to edit, we get a ViewState error because the control tree changes.
We need to enable ViewState in presentation mode, so we need to fix this transition error.
We have tried disabling ViewState during the postback, then programmatically posting back again and re-enabling it, but this causes validation issues with MOSS, so it does not appear to be an option.
Ideas?
Have you tried the clear method on the property bag ;-)
Are you changing the control tree with some advanced information? Typically on the postback you'll rebuild the same control tree before the event handlers for buttons fire (view state should validate at this point) and then handle the event - which may include clearing out previous parts of the control tree.

Resources