What is view state in ASP.Net?

View state is a built-in structure in ASP.NET for automatic retention of page and control state, which means you don’t need to do anything about getting back the data of controls after posting page to the server.

Microsoft ASP.NET Web Forms pages are capable of maintaining their own state across multiple client round trips. When a property is set for a control, the ASP.NET saves the property value as part of the control’s state. To the application, this makes it appear that the page’s lifetime spans multiple client requests. This page-level state is known as the view state of the page. In Web Forms pages, their view state is sent by the server as a hidden variable in a form, as part of every response to the client, and is returned to the server by the client as part of a postback.

The view state is a key element of an ASP.NET page because it is the primary means to persist the state of the Web server controls. Whenever the page posts back, the state is restored, updated using the current form parameters, then used to run the postback event handler. Normally, the view state is a hashed string encoded as Base64 and stored in a hidden field called __VIEWSTATE. In this way, the view state is not cached on the client, but simply transported back and forth with potential issues both for security and performance. Since it is performance overhead, you need to decide properly when and where you should use viewstate in your webform

The viewstate is enabled by default for all the server controls.

Example of putting the Data Set in view state
The example stores a dataset in view state on a Web Forms page. It first creates an XML representation of the dataset and then stores the XML as a string.

Dim sw As New System.IO.StringWriter
ViewState("dsNorthwind") = sw.ToString()

This example retrieves the DataSet from a view state.
if (Page.IsPostBack)
   System.IO.StringReader sr =
      new System.IO.StringReader((string)(ViewState["dSet"]));

Note: As a rule, in the Web Applications, storing datasets in view state only if the dataset is very small. Large datasets can significantly increase the size of the ASPX page, resulting in slower download to the client and slower posting.

Normally, the view state is a hashed string encoded as Base64 and stored in a hidden field called __VIEWSTATE

With Regards,
UVN PardhaSaradhi