[Solid Quality ASP.NET] Bits and pieces of your viewstate
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.