.netCoders Contact Us
Search:

Navigation

One of the significant improvements in ASP.NET is the use of view state and postback events. In this model, you can eliminate the code necessary to retrieve form variables through the query string and instead can reference the object directly. This puts ASP.NET more in line with Windows programming, where elements on a form maintain their values when the user clicks the submit button and some processing takes place.

For those unfamiliar with the term, ViewState is a hidden form field containing encrypted data representing the values in form fields. ASP.NET can interpret this ViewState and populate form fields with the correct values. To see the view state, load a Web Form in your browser, and select View Source. You should see a form field like the following:

<form name="form1" method="post" language="javascript" onsubmit="ValidatorOnSubmit();" id="form1">
<input type="hidden" name="__VIEWSTATE" value="dDwxODI1OTAwMDQxO3Q8O2w8aTwwPjs+O2w8dDw7bDxpPDI+Oz47bDx0PDtsPGk8MD47PjtsP
HQ8O2w8aTwwPjtpPDI+O2k8ND47PjtsPHQ8O2w8aTwwPjs+O2w8dDw7bDxpPDE+Oz47bDx0PHA8cDxsP
FRleHQ7PjtsPEMjIENvbG9yIENvZGVyOz4+Oz47Oz47Pj47Pj47dDw7bDxpPDA+Oz47bDx0PDtsPGk8M
T47PjtsPHQ8cDxwPGw8VGV4dDs+O2w8Lk5FVCBRdWl6Oz4+Oz47Oz47Pj47Pj47dDw7bDxpPDA+Oz47b
Dx0PDtsPGk8MT47PjtsPHQ8cDxwPGw8VGV4dDs+O2w8QWJvdXQ7Pj47Pjs7Pjs+Pjs+Pjs+Pjs+Pjs+P
js+Pjs+4TnJDSj3HYc+S3YV2PbEgukVwFw=" />
If you are looking for a solution whereby you can share form field values across multiple pages, see our section on Session State.

Managing the View State

ASP.NET handles the ViewState by default, but sometimes you do not want ViewState enabled, as in the case where you are posting to another page, and not using the post-back mechanism. In these cases, the ViewState just makes your page larger. You can turn ViewState off at the control or page level using the EnableViewState attribute. For controls, set this attribute to false, as in the following example:
<asp:DropDownList id="States" EnableViewState="false" runat="server" />
To turn the ViewState off for the entire page, include this attribute in the Page directive, as in the following example:
<@ Page EnableViewState="false" %>