Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Persistence for ASP.NET Controls


The Debug Attribute

ASP.NET pages and controls can store some state variables into a persistent medium known as the ViewState. Whatever value you place in the ViewState is guaranteed to survive across requests. Typically, server controls use the ViewState to cache the values of some of their properties. For example, the textbox control stores to the ViewState, and retrieves from it the current value of properties such as ReadOnly, MaxLength, and Text. Each and every control sees its own ViewState object and uses it as a general-purpose cargo collection. When the page is rendered to HTML, though, the contents of all view states of all controls in the page is flushed into a single object. This object is then serialized to a binary format, encoded to Base64 and copied to a hidden field.

When the page posts back, the ASP.NET runtime retrieves and decodes the ViewState and uses its values to reinitialize the various controls on the page. In other words, the ViewState represents the state of the page the last time it was served on the server.

So far, so good. The rub with the ViewState lies in the fact that it adds a significant burden to any page served to the browser. For complex ASP.NET pages, the extra overhead of the ViewState can easily reach 10 KB. Not only do you risk downloading an oversized page, but the extra bytes stored in the ViewState are completely useless on the client. More, if you modify the ViewState on the client side, the page will throw an exception once returned back to the server.

To keep your page lean and mean, you can disable the ViewState throughout the page or just for a few controls. Most ASP.NET controls can do without the ViewState even though this requires some adjustments in the programming style. Some controls, though, just can't do without the ViewState. Technically speaking, the ViewState is not a strict requirement for ASP.NET controls. In practice, some controls need to persist data across postbacks, and nonpersistent data would result in a loss of functionality. For example, the TextBox control can't raise the TextChanged event if the ViewState is disabled. The TextChanged event is fired in response to a detected difference between the new and the old value of the Text property. But if the old value is not cached, there's no way for the control to detect the difference.

Originally, the ViewState was introduced as a way to let applications store dynamic data between one or more requests. It was not designed to be a sort of control-state container. A couple of years of real-world experience proved two key things. One is that disabling the ViewState may break the code because some controls stop working the way you are accustomed to. Secondly, ASP.NET controls do need a mechanism to persist their control state. Obviously, control developers need an alternative to the ViewState for storing the state of a control.

Until ASP.NET 2.0 bring some good news on this topic, there's one neat and elegant workaround for the issue-—using a sort of "parallel" ViewState. Create a control-specific collection in which you store all the values you want to persist. Next, when the control is about to render, you serialize the contents of this collection to a hidden field. The content of the hidden field will be retrieved and processed when the page posts back during the preloading step of the page. A good place to rebuild the control state is in the LoadViewState override.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Magazine and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected]


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.