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” >