ASP.NET Web Forms - Maintaining ViewState
By maintaining the ViewState of objects in your Web Form, you can save a significant amount of coding work.
Maintaining ViewState
In classic ASP, when a form is submitted, all form values are cleared. Suppose you submit a form with a lot of information, and the server returns an error. You have to go back to the form to correct the information. You click the back button, and then what happens... all form values are cleared, and you have to start everything all over again! The site did not maintain your ViewState.
In ASP.NET, when a form is submitted, the form along with its values appears in the browser window. How is this done? This is because ASP.NET maintains your ViewState. ViewState indicates its state when the page is submitted to the server. This state is defined by placing a hidden field on each page with a <form runat="server"> control. The source code is shown below:
Maintaining ViewState is the default setting for ASP.NET Web Forms. If you do not want to maintain ViewState, include the directive <%@ Page EnableViewState="false" %> at the top of your .aspx page, or add the attribute EnableViewState="false" to any control.
Take a look at the following .aspx file. It demonstrates the "old" way of working. When you click the submit button, the form values will disappear:
Example
Here is the new ASP.NET way. When you click the submit button, the form values will not disappear:
Example
Click on the view source code in the right frame of the example, and you will see that ASP.NET has added a hidden field to the form to maintain ViewState.