How to set view state for a server control? Enableviewstate property?

The enableviewstate property of a server control indicates whether the server control persists its viewstate. It also controls the viewstate behavior of the child controls within it.

So what is Viewstate? – Viewstate is the property of a server control that groups all the other property values of the control, so that the entire set of property values is preserved across multiple HTTP requests. The enableviewstate property when set to true, makes the viewstate property persist all the other property values.

How does viewstate work? – The viewstate values are passed as an HTML hidden input element when HTTP requests are invoked. To see how a viewstate variable’s value looks like, right click any aspx page and click view source, you will find something like this…
type=”hidden” name=”__VIEWSTATE” id=”__VIEWSTATE” value=”/wEPDwULLTEw.. .. .. .. 2FzcF9hc3AubmV0LmFzcHgfB==”
An instance of the StateBag class is created to store the property values.

There are scenarios when a Viewstate is set to false. For example, say a database request is loaded to a server control, then the size of the database values may be humongous, for which the Viewstate is set to false.
Example of setting viewstate of a server control …

<asp:TextBox id=”txtName” runat=”server” text=”” enableviewstate=”true” >

What is the use of “EnableViewState” property?

Enable View State turns on the automatic state management feature that enables server controls to re-populate their values on a round trip without requiring you to write any code. This feature is not free however, since the state of a control is passed to and from the server in a hidden form.

What does the “EnableViewState” property do? Why would I want it on or off?

Enable ViewState turns on the automatic state management feature that enables server controls to re-populate their values on a round trip without requiring you to write any code. This feature is not free however, since the state of a control is passed to and from the server in a hidden form field. You should be aware of when ViewState is helping you and when it is not. For example, if you are binding a control to data on every round trip (as in the datagrid example in tip #4), then you do not need the control to maintain it’s view state, since you will wipe out any re-populated data in any case. ViewState is enabled for all server controls by default. To disable it, set the EnableViewState property of the control to false.

Explain what is the purpose of using EnableViewState property?

  • When a page is postback, then it allows the page to save the users input on a form.
  • It also saves all the server side values as a hidden value on the page before sending that page to the clients for a particular given control in the ViewState.
  • When a page is posted back to the server, at the same time the server control is recreated within the state stored in ViewState.

Explain the aim of using EnableViewState property.

It allows the page to save the users input on a form across postbacks. It saves all the server side values for a given control into ViewState, which is stored as a hidden value on the page before sending the page to the clients browser. When the page is posted back to the server, the server control is recreated with the state stored in viewstate.

Difference between ViewState and ControlState.

ViewState and ControlState are both mechanisms used in ASP.NET for maintaining data across postbacks. Both are preserved in a hidden field known as _VIEWSTATE.
The differences are:

  • ViewState can be disabled while the Control State cannot be disabled.
  • ViewState is implemented by using EnableViewState property of a control to true.

Control State works even when EnableViewState is off.

To use Control State (for example in a custom control) we have to override OnInit method,call RegisterRequiresControlState method in OnInit method and then override the SaveControlState and LoadControlState methods.

Custom Controls code is written in classes derived from WebControl.

  • Control State is used for small data only.

eg: maintain clicked page number in a GridView even when EnableViewState is off

How to disable view state on a page?

Set EnableViewState=”false” in page directives.

Example:

<%@ Page EnableViewState=”false” %>