[Solid Quality ASP.NET] Bits and pieces of your viewstate

Published 28 September 05 04:39 PM | despos

Everybody complains about the size of the viewstate. And with good reason. I'm certainly not here to say that there's a genial trick to vaporize the viewstate that nobody knows on the face of the earth. However, one of the problems you may experience out of a too-large viewstate is that it is too large to fit into the request. Sometimes the underlying browser is not capable of carrying all those bytes back and forth. As a result, the contents of the view state is truncated and the application fails. This is particularly likely to happen on pretty simple Web browsers such as WebTV and PDAs. In addition to remove the viewstate altogether, is there other you can try?

In ASP.NET 1.x, you could create a custom page class, override the LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium methods and make them read and write viewstate contents in the session state, in a database, or in any other data store on the server. Neither a mission-impossible task, nor an easy one. The devil is in the details. You must be absolutely sure that a particular page retrieves its own viewstate. Not all solutions are good at this.

So what's new in ASP.NET 2.0? There's a new attribute in the <pages> section named maxPageStateFieldLength

<pages maxPageStateFieldLength="5" />

It indicates the maximum size allowed for the viewstate. If the real size exceeds the value set in the web.config file the viewstate is sent in chunks. Here's how:

input type="hidden" name="__VIEWSTATEFIELDCOUNT" id="__VIEWSTATEFIELDCOUNT" value="16" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPD" />
<input type="hidden" name="__VIEWSTATE1" id="__VIEWSTATE1" value="wUKLT" />
<input type="hidden" name="__VIEWSTATE2" id="__VIEWSTATE2" value="I2MjI" />
<input type="hidden" name="__VIEWSTATE3" id="__VIEWSTATE3" value="3NDkw" />
<input type="hidden" name="__VIEWSTATE4" id="__VIEWSTATE4" value="NA9kF" />
<input type="hidden" name="__VIEWSTATE5" id="__VIEWSTATE5" value="gICAw" />
<input type="hidden" name="__VIEWSTATE6" id="__VIEWSTATE6" value="9kFgI" />
<input type="hidden" name="__VIEWSTATE7" id="__VIEWSTATE7" value="CCg88" />
<input type="hidden" name="__VIEWSTATE8" id="__VIEWSTATE8" value="KwANA" />
<input type="hidden" name="__VIEWSTATE9" id="__VIEWSTATE9" value="GRk7N" />
<input type="hidden" name="__VIEWSTATE10" id="__VIEWSTATE10" value="FNJbP" />
<input type="hidden" name="__VIEWSTATE11" id="__VIEWSTATE11" value="PZl6t" />
<input type="hidden" name="__VIEWSTATE12" id="__VIEWSTATE12" value="C+gkP" />
<input type="hidden" name="__VIEWSTATE13" id="__VIEWSTATE13" value="/XMyn" />
<input type="hidden" name="__VIEWSTATE14" id="__VIEWSTATE14" value="MRGPs" />
<input type="hidden" name="__VIEWSTATE15" id="__VIEWSTATE15" value="=" />

By default, the attribute is set to -1 which means that no maximum size is defined. Therefore, things work like in ASP.NET 1.x by default. The final byte count of the page is even a bit higher so THIS IS NOT a way to optimize the download time. It is a simple, effective, and inexpensive way of sending large viewstates over limited channels.

 

Comments

# Paul Wilson said on September 28, 2005 11:38 AM:

You can do this in ASP.NET v1 also, although of course it takes a little bit of code (but not much):
http://weblogs.asp.net/pwilson/archive/2003/08/21/24867.aspx

# Andrew Robinson said on September 28, 2005 12:19 PM:

Dino,

I don't understand... doesn't the entire text/html of the page just get streamed over a single http tcp/ip channel / port / connection? Why would breaking this into chunchs have an advantage with this single channel? Or are the channels that we are talking about on the browser side while parsing HTML?

Thanks,

-Andrew

# Eric Newton said on September 28, 2005 05:57 PM:

I can't imagine this actually helping anything, unless there's some small devices that have a hard limit on the length of form fields in its little browser.

It would probably be something like 64 or 256.

But note all the extra bytes of redefining more viewstate hidden fields, and you can see how this method doesnt really save bytes, but in fact adds even more payload.

# Geoffrey Samper said on September 29, 2005 06:58 AM:

Like Dino says in asp.net 1.x you could store the viewstate on the server by overriding the methods LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium. In Asp.NET 2.0 they did provide us a solution so that we can choice where we want to store the viewstate and the controlstate check out the documentation http://msdn2.microsoft.com/en-us/library/system.web.ui.PageStatePersister . By default the viewstate is stored in a hidden field but if you want to store it in the session it is possible check out here http://msdn2.microsoft.com/en-us/library/system.web.ui.SessionPageStatePersister.

# Tan said on July 31, 2007 10:43 AM:

WELL, I did <pages maxPageStateFieldLength="5000" /> in the web.config I can see that it viewstate contain multiple line but my problem is still there

I put 5000 because the viewstate is huge I am sure that the problem is view state as sometime it works and sometime it does not

when I tested on diferrent machines ( one of the machine which is always not working) give me error Invalid viewstate: Missing field: __VIEWSTATE81

Could you please advice me what sort of option which I can try

Thanks

# diller said on September 8, 2007 07:07 PM:

HI  good suite can you look on my sites

<a href="allfunds.free-site-host.com/child-defense-fund/index.html">child defense fund</a>

[URL=allfunds.free-site-host.com/child-defense-fund/index.html]child defense fund[/URL]

child defense fund - allfunds.free-site-host.com/child-defense-fund/index.html

<a href="allfunds.free-site-host.com/fund-raising-jewelry/index.html">fund raising jewelry</a>

[URL=allfunds.free-site-host.com/fund-raising-jewelry/index.html]fund raising jewelry[/URL]

fund raising jewelry - allfunds.free-site-host.com/fund-raising-jewelry/index.html

<a href="allfunds.free-site-host.com/money-market-fund/index.html">money market fund</a>

[URL=allfunds.free-site-host.com/money-market-fund/index.html]money market fund[/URL]

and on this

money market fund - allfunds.free-site-host.com/money-market-fund/index.html

<a href="allfunds.free-site-host.com/fund-raising-letter/index.html">fund raising letter</a>

[URL=allfunds.free-site-host.com/fund-raising-letter/index.html]fund raising letter[/URL]

fund raising letter - allfunds.free-site-host.com/fund-raising-letter/index.html

<a href="allfunds.free-site-host.com/index-funds/index.html">index funds</a>

[URL=allfunds.free-site-host.com/index-funds/index.html]index funds[/URL]

index funds - allfunds.free-site-host.com/index-funds/index.html

<a href="allfunds.free-site-host.com/college-fund/index.html">college fund</a>

[URL=allfunds.free-site-host.com/college-fund/index.html]college fund[/URL]

college fund - allfunds.free-site-host.com/college-fund/index.html

<a href="allfunds.free-site-host.com/non-profit-fund-raising/index.html">non profit fund raising</a>

[URL=allfunds.free-site-host.com/non-profit-fund-raising/index.html]non profit fund raising[/URL]

non profit fund raising - allfunds.free-site-host.com/non-profit-fund-raising/index.html

<a href="allfunds.free-site-host.com/easy-fund-raising-idea/index.html">easy fund raising idea</a>

and:

[URL=allfunds.free-site-host.com/easy-fund-raising-idea/index.html]easy fund raising idea[/URL]

easy fund raising idea - allfunds.free-site-host.com/easy-fund-raising-idea/index.html

<a href="allfunds.free-site-host.com/state-fund/index.html">state fund</a>

[URL=allfunds.free-site-host.com/state-fund/index.html]state fund[/URL]

state fund - allfunds.free-site-host.com/state-fund/index.html

Leave a Comment

(required) 
(required) 
(optional)
(required)