Shravan Kumar Kasagoni

Skip Navigation
What is View State?

What is View State?

 /  Leave a response

HTTP protocol is stateless protocol. Each time you request a web page from the web server, from a web application perspective, you are a completely new person. HTTP protocol can’t remember the data of a request once it is submitted to the web server so we can’t use the request data of one request in another request.

How ASP.NET manages the transcend behaviour of the HTTP?

Look at the below example:


<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
  protected void btnSubmit_Click(object sender, EventArgs e) {
    TextBox1.Text = (Int32.Parse(TextBox1.Text)+1).ToString();
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title>View State</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <div>
        <p>
          <asp:TextBox runat="server" ID="TextBox1" 
            Text="2" Height="20px" Width="150px" />
        </p>
        <asp:Button runat="server" ID="Button1" Text="Submit"
          OnClick="btnSubmit_Click" Height="25px" 
          Width="100px" />
      </div>
    </form>
  </body>
</html>

View State
View State

In the above example I have assigned a value 2 to the TextBox Control. Each time I click on the button control, the value displayed in TextBox Control is incremented by one. As our protocol is stateless every time the value of the control should remain same but it is incremented by 1.

How does the TextBox control preserves its value across the post-backs to the server?

The ASP.NET Framework uses a technique called View State.

In the browser go to View Menu => Source, In the .aspx page source we can find a hidden form field named _VIEWSTATE that looks like this.


<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
  value="/wEPDwUKMTkyNw1DIRPrITkOmqdok=" />

This hidden form field contains the value of TextBox control’s Text property & the values of any other control properties that are stored in View State. When the page is posted back to the server, the ASP.NET excludes this string and recreates the values of all the properties stored in View State. In this way, ASP.NET preserves the state of the control properties across the postbacks to the web server.

Write a response

Your email address will not be published. Required fields are marked *

All code will be displayed literally.