How is ViewState information persisted across postbacks in an ASP.NET webpage?

Using HTML Hidden Fields, ASP.NET creates a hidden field with an ID=”__VIEWSTATE” and the value of the page’s viewstate is encoded (hashed) for security.

Why should you avoid the excessive use of ViewState in Asp.Net web page?

Automatic state management is a 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.

Since the view state data resides in a hidden form field (__VIEWSTATE); ViewState increases the size of page and results in slow loading of page at browser. 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 , 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, as in the following example:

< asp:datagrid EnableViewState=”false” datasource=”…” runat=”server”/>
You can also turn ViewState off at the page level. This is useful when you do not post back from a page at all, as in the following example:

< %@ Page EnableViewState=”false” %>
Note that this attribute is also supported by the User Control directiv

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

Can dataset be stored in a viewstate?

Yes, a dataset may be stored in a viewstate. However, a viewstate is passed to the server as a hidden variable. Precaution should be taken that the dataset size is not too big, otherwise such an activity will take a toll on both the client and server system resources, and ultimately slow down rendering of the data on the client system. Also note that the viewstate data may not be sent to another aspx page, as thats a limitation of a viewstate object.

How to Manage State in ASP.Net?

There are several ways to manage a state.

  • ViewState
  • QueryString
  • Cookies
  • Session
  • Application

For a blank ASP.NET Page or ViewState Disabled page, will there be any value stored in the _VIEWSTATE field?

The reason is that ASP.NET Page itself stores some bytes of information into the _VIEWSTATE field.

Note : The Viewstate of an ASP.NET page is created during the page life cycle, and saved into the rendered HTML using in the “__VIEWSTATE” hidden HTML field.

 

What are the Benifits of Using ViewState?

a).No server resources are required because state is in a structure in the page code.
b).Simplicity.
c).States are retained automatically.
d).The values in view state are hashed, compressed, and encoded, thus representing
e).A higher state of security than hidden fields.
f).View state is good for caching data in Web frame configurations because thedata is cached on the client.