View State in ASP.NET

How to Maintain View State in ASP.NET:

Importance of View State:

When a form is submitted in classic ASP, all form values are cleared. Suppose you have submitted a form with a lot of information and the server comes back with an error. You will have to go back to the form and correct the information. You click the back button, and what happens? All form values are CLEARED, and you have to start all over again! The site did not maintain your View State in ASP.NET.

When a form is submitted in ASP.NET, the form reappears in the browser window with all form values. How come? This is because View State in ASP.NET is maintained.

To UnderStand the concept of view state and what happen if we don’t use view state.

Step 1: Open Visual Studio.
Step 2: Click on New Project.
Step 3: Select Visual C# from the pop-up window. Click ASP.Net Web Applications set a name and location if you want to save it somewhere else instead of the default location and click OK.
Step 4: A new pop-up window will open. Click empty template, check web form and click OK.
Step 5: Now the project will load and start. In this window on the right side of the window in the solution explorer (if it is not here, pick it by clicking view or press alt+v) right click on webapplication1 you created. Select Add and then select web form.

Add a new web form in Visual Studio
Add a new web form in Visual Studio

Step 6: Write the name of the new web page.

name for web form visual studio
Name of the web forms visual studio

Step 7: Now the web page appears and for its design view, select the Design option at the mid bottom and open the toolbox window by clicking VIEW or alt+v from the Tool bar and select tool box from here.
Step 8: Design your web page by just dragging two labels, two textboxes and two buttons from the Toolbox.

ASP Code:

Simple asp.net web form
Simple asp.net web form

Step 9: Write click anywhere in the editor, select view code and code for buttons, onclick event handlers or double click the buttons separately. It will automatically generate button handlers for the clicked button.

C# Code:

ASP button handlers
ASP button handlers

Step 10: Now Click on Google Chrome or any other selected browser to run this project or press F9.
Step 11: Web Page will Appear in the browser.

Custom ASP.net webpage
Custom ASP.net webpage

Step 12: Enter Values in the text box and click submit. The values will clear because after assigning these values to string variables fname and lname we set textboxes to null and now if we try to restore these values by clicking restore they will not restore just because of view state is not maintained and the browser does not remember the last values of the page.

Maintain View State:

Step 1: Now to maintain view state change the code.

Code to Maintain View State:

Now, after maintaining view state when you click restore button, the last values will come back because the browser remembers the last values.

Features of View State:

  • Saves the values without using session after postback calls.
  • Saves the values of the pages and control properties after the page is reloaded.
  • It also allows you to store values and data in a database for example SQL Server or any other source like data sets.

See Also

Session State

Application state