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.

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

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:
1 2 3 4 5 6 7 8 9 10 11 12 |
<body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="First Name: "></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Restore" /> </div> </form> </body> |

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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } string fname, lname; protected void Button1_Click(object sender, EventArgs e) { fname = TextBox1.Text; lname = TextBox2.Text; TextBox1.Text = TextBox2.Text = null; } protected void Button2_Click(object sender, EventArgs e) { TextBox1.Text =fname; TextBox2.Text = lname; } } |

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.

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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } string fname, lname; protected void Button1_Click(object sender, EventArgs e) { ViewState["fname"] = TextBox1.Text; ViewState["lname"] = TextBox2.Text; //after clicking on Button TextBox value Will be Cleared TextBox1.Text = TextBox2.Text = string.Empty; } protected void Button2_Click(object sender, EventArgs e) { if (ViewState["fname"] != null) { TextBox1.Text = ViewState["fname"].ToString(); } if (ViewState["lname"] != null) { TextBox2.Text = ViewState["lname"].ToString(); } } |
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.