Search code examples
asp.net.netviewstate

What is the ViewState in ASP.NET?


Can anybody explain ViewState(in ASP.NET) as succintly as possible?


Solution

  • ViewState is the mechanism that allows state values to be preserved across page postbacks.

    The web is stateless. But in ASP.NET, the state of a page is maintained in the page itself automatically. This is done Using ViewState. In ViewState the values are base 64 encoded and saved in hidden controls.

    When you view the page source (in your browser) of a page the uses ViewState, you may see this hidden ViewState input which will look something like this:

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

    This single hidden field contains all the viewstate values for all the page controls.

    Because ViewState is (by default) sent to the client browser and then returned to the server in the form of a hidden input control on your page, storing a significant amount of data in ViewState can increase your page size and can affect your page performance.

    To disable ViewState for a control, you can set the EnableViewState property to false.