View state in ASP.NET

View State is ASP.NET

View State is one of the state management technique.

View State is the mechanism that allows state values to be preserved across page postbacks. Because of the stateless nature of the web pages, regular page member variables will not maintain their values after postback.

When we need a page variable to maintain its value across page post backs, we can use ViewState to store that value. Values stored in ViewState will be serialized and sent to the client browser as a value of a hidden form input. When you view the page source (in your browser) of your page you will find that it uses View State, which looks something like this:

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. When ViewState is disabled for any control, it will also automatically be disabled for all child controls of that control.

Example:

ViewState["vaibhav"] = myValue;
Response.Write(ViewState["vaibhav"]);

Advantages:
A. Easy to store and maintain page level data.
B. Can be set at control level.
C. Encrypted.

Disadvantages:
A. Makes a page heavy, if a good amount of data is stored into it.

admin