Understanding Viewstate Management in ASP.NET
By default the viewstate is enabled for a webform. Hence when you submit a webform the values of the controls are sent to the server in a hidden field __ViewState. These values can be accessed after the PreInit event for the page and before the PreRender event of the page. During postback the values of the viewstate are bound to the controls and hence the controls retain their property values between postbacks. Since the values of the controls are stored in the viewstate hiddenfield and sent to the client, the size of the file is increased by a few kB. If more number of controls viewstate is enabled then the length of the hidden field is going to increase and the page will load slowly. Care should be taken to find out the controls which need their viewstate to be enabled. Any control whose value is just for viewing can have its viewstate disabled to enable fast loading of the web page. To enable viewstate at the control level, you need to set the property EnableViewState to True. This property of a control is set to false to disable viewstate for that control. If disabled, the property values of the control are not stored in the viewstate. Hence the values are not retained in that control during postback. The code below shows the EnableViewState property of a control set to False. <asp:TextBox EnableViewState="False" id="TextBox1" runat="server"> </asp:listbox> Viewstate can be enabled or disabled even at the page level. To do this, the EnableViewState attribute has to be added to the page directive, as given below: <%@ Page EnableViewState="False" Language="vb" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication.WebForm1"%> Viewstate can be altered for all the pages in a web application by making some changes in the web.config file of the application as shown below. <configuration> In the web.config file set the enableViewState attribute of the pages element to True to enable viewstate for all the pages in the web application. Since __ViewState hidden field contains Base64 encoded values, it is not readable by humans. A sample of the viewstate value is given below. <input type="hidden" name="__VIEWSTATE" value="dDwxMzkwODI1NzQ5Ozs+o6BdqddLxQEge5DidKFq1kh7xRc=" /> It is possible to write some simple routines to convert the Base64 values back to text and then find out the property values of the controls. Hence it is not advised to store important information in the viewstate. A person can change the values of the viewstate if he can convert the Base64 values. ASP.Net provides a mechanism to check whether the viewstate values have been changed. An property called EnableViewStateMac helps to check whether there is any change in value of the viewstate. To check for any change in the viewstate value, the EnableViewStateMac property has to be set to True. The code given below shows how this is set in the page directive. <%@ Page EnableViewStateMac="True" Language="vb" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication.WebForm1"%> Since we know that the viewstate value is in a hidden field, it is possible to manipulate the viewstate value programmatically. You can add values to the viewstate and retrieve those values also. To add values to the viewstate during some event, you need to assign the values to the viewstate as you would manipulate a session variable or an application variable as shown below: ViewState("UserName") = "John Peter" To retrieve the value set in the viewstate you may even use a Response.Write statement to write the values to the page like: Response.Write("The User Name is " & ViewState("UserName")) Thus Viewstate
is an important feature that is added in ASP.Net, which retains the current
state of the controls on a webform and bind them to the controls when
a postback of the page occurs. Viewstate has to be used only if necessary,
since it is a performance overhead for the page and it may slow down the
loading of the page since the viewstate values are also transported to
the server and back to the client.
|