HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

View State expalined in detail in ASP.NET
To understand viewstate better, drag and drop textbox (input(text)) from HTML section from the tool box. Next to the textbox drop a button control from standard section from the toolbox.Run the page by pressing ctrl+F5. Now type your name in the textbox and press the button. When the page refreshes, the value that we have typed in the textbox is lost.

Now let us understand why is this happening. Web applications work on HTTP which is a state less protocol. What do we mean by state less protocol. When the page is rendered on the client browser, the user types his name into the textbox and when we press the button control, the page posts back to the server and the entire page is recreated. Even the controls on the page are recreated. So a new instances of the control are sent back to the user. This is the reason why the textbox is loosing the value that we have typed in.

Now try this. Drag and drop a textbox and a button from standard controls section from the toolbox.Run the page. Type your name and press the button. If you observe this textbox does not loose the value that we have typed into the textbox. This is because the ASP.NET textbox that we have dragged from the toolbox maintains its state, where as the textbox that we have dragged from the HTML section from the toolbox does not retain its state. So, Web server controls retain their state where as HTML controls donot retain their state across a post back to the page.

Now let us understand how the web server controls are able to retain their data even after post back event. When we enter some data into the textbox and press the button. The page is posted back. While the page is posted back to the server, along with the page, the data that is present in the controls on the page is stoed in a hidden field on the webform called _ViewState. This hidded field _ViewState is also sent to the webserver. One thing we need to understand here is that, the data is not stored in the _ViewState hidden field as a plain text. Instead the data is base 64 encoded and then saved into this hidden field. This is for security reasons. To look at this hidden filed, after our sample page is displayed in the browser - click on View - > Source in the internet explorer window. You will find our hidden _ViewState filed as shown in the figure below(Look for the text highlighted in blue).
Image Missing 
Image showing "_ViewState" hidden field

Now when we click the button on the webform, the page will post back. Then the webserver will recreate the controls, in our case both the button and the textbox controls. After the controls are created then the data from the hidden _ViewState field is retrieved and then inserted into the textbox. Then the webserver will generate the required HTML and send it to the client browser who has made the request.

Even the variables declared on the page will loose their data. As an example drag and drop a textbox and button control on to the webform as shown below.

Declare a class level variable of type integer and initialize it to 0. On the page_load event handler set the textbox text to the value in the "Counter" variable. In the Button1_click() event handler increment the value of the Counter 1 and assign the resulting value to the text property of the text as shown in the code snippet below.
public partial class Question : System.Web.UI.Page
{
     //Class Level Variable
      int Counter = 0;
      protected void Page_Load(object sender, EventArgs e)
      {
           //During the initial get request we populate the textbox with the counter value
             if (!this.Page.IsPostBack)
             {
                  TextBox1.Text = Counter.ToString();
             }
      }
 
      protected void Button1_Click(object sender, EventArgs e)
      {
           //Increment the counter value by 1 every time we click the button
             Counter = Counter + 1;
             TextBox1.Text = Counter.ToString();
      }
}

When you run this Page, the web server creates an instance of the webform. Then creates the local variable "Counter" and initializes it to 0. Then on the Page load the value of the counter is assigned to the text property of the textbox. After this the webserver will generate the required HTML and send it to the client and the webform object is garbage collected. As a result of this the webserver will loose all the controls and variables on the webform.

Now when we press the button on the the webform. A new request for the webform is sent to the server. The webserver will now create another new instance of our webform. Creates our variable "Counter" and set its value to 0. Then the button click event is processed which will increment the value of the "Counter" variable from 0 to 1. This incremented value is then assigned to the textbox. We see the incremented value. After the value 1 is displayed on the webform, if you click the button again the value will not get incremented beyond 1.

This is because every time we click the button the webform is recreated, the counter variable is recreated and assigned to 0 and then incremented to 1. This is because of the state less nature of the webform. If you want to retain the value of the Counter variable then, you have to save it in a ViewState variable as shown in the below code.
public partial class Question : System.Web.UI.Page
{
     //Class Level Variable
      int Counter = 0;
      protected void Page_Load(object sender, EventArgs e)
      {
           //During the initial get request we populate the textbox with the counter value
             if (!this.Page.IsPostBack)
             {
                  TextBox1.Text = Counter.ToString();
                 //Assign the Counter Variable value into a ViewState variable
                   ViewState["Count"] = Counter;
             }
      }
 
      protected void Button1_Click(object sender, EventArgs e)
      {
           //Retrieve the value of the Counter variable that is present in the ViewState into a local variable
             int i = (int)ViewState["Count"];
           //Increment the value of local variable by 1
             i = i + 1;
           //Save the incremented value into the ViewState variable
             ViewState["Count"] = i;
           //Display the incremented value in the TextBox
             TextBox1.Text = i.ToString();
      }
}

Points to remember about ViewState:
  • ViewState is basically used to retain the data of webcontrols on the webform across postbacks to the same page.
  • ViewState is base 64 encoded.
  • You can store any type of data in a ViewState variable. Example - Integers,strings,boolean,objects etc.
  • When you retrieve data from the ViewState field, you will have to type cast the data to appropriate type as shown here - int i = (int)ViewState["Count"]
  • ViewState of a Page is available only within that page. Viewstate of one page is not available in another page.
  • You can turn off viewstate of a control by setting the enable viewstate property of the control to false
  • ViewState of the page is saved in a hidden field called _ViewState.
  • You can disable ViewState at Control,Page or Application Level.

Njoy Programming
   ByPrasad Cherukuri