Introducing ViewState in ASP.NET


Introduction

  • ViewState is an ASP.NET feature that provides for retaining the value of page and control properties that change from one execution of a page to another.
  • Before ASP.NET sends a page back to the client, it determines what changes the program has made to the properties of the page and its control. These changes are encoded in to a string that's assigned to the value of hidden input field named-ViewState.
  • When the page is posted back to the server, the ViewState field is sent back to the server along with the HTTP request. Then ASP.NET retrieve the property value from the ViewState field and uses them to restore the page and control properties.
  • ASP.NET also uses ViewState to save the values of the page properties it uses, such as IsPostBack.
  • ViewState is not used to restore the data entered by the user in to a textbox or any other input control unless the control responds to change events.
  • If ViewState is enabled for the data-bound control, the control will not be rebound when the page is reposted. Instead, the control's values will be restored from ViewState.
  • ViewState makes a page heavy, it is the drawback of ViewState.

How to enable ViewState for selected controls

  • Set the EnableViewState property of the page and controls whose ViewState you want to enable to True.
  • Set the ViewStateMode property of the page (Page directive) is Disabled.
  • Set the ViewStateMode property of the controls whose ViewState you want to enable to Enabled.

ViewState usage by controls

image1.png

Two cases when you may want to disable ViewState

  • When restoring the control properties for the page affects the way you want the form to work, you may want to disable ViewState for one or more controls. Otherwise you can modify your code so the page works without turning ViewState off.
  • When the size of  the ViewState field gets so large that it affects performance, you may want to disable ViewState for one or ore controls or for an entire page. 

How to disable ViewState

  • To disable ViewState for a control, set the control's EnableViewState property to false. Like as
    <asp:DataGrid EnableViewState='false' .../>                                                
  • To disable ViewState for an entire page, set the EnableViewState property of the page (page directive) to false. That disable ViewState for all the controls on the page. Like as
     <%@Page EnableViewState='false' %>
  • You can disable ViewState at the application level if none of your pages post back to themselves
    <!-- inside web.config for your app -->
    <Pages EnableViewState='false'/>

Although ViewState is designed to automatically save page and control property values across round trips to the browser, you can also add your own data to ViewState.

Like the session state object, the ViewState object contains a collections of key/value pair that represent the items saved in ViewState. To access this object , you use the ViewState property of the page.

Comparison between Session, Cookies and ViewState

There are three concept for maintaining the state of the page on the srever i.e. Session, Cookies and ViewState. But there are some differences among them. The differences table of these concepts are shown below.

image2.png

Summary

So ViewState is used by controls to retain state across post-backs. It is not always necessary, depending on control usage. ViewState is stored as a base64-encoded string and it can be encrypted. ViewState has some drawback i.e. it makes a page heavy.

Some Helpful Resources