Browse DevX
Sign up for e-mail newsletters from DevX


Client- and Server-side Solutions to Managing State in ASP.NET : Page 2

Whether you are building a traditional Windows application or a Web-based application, state is what an application knows about the user, their current interaction with the application, and other pieces of global information. Get a full assessment of the best techniques for managing state.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Client-Based Technique #1: ViewState
One issue you likely encounter using Web Form control values is that many browsers do not support blanks in a query string.
I will start with the simplest client-based state management technique to help the server "remember" things about a returning Web page, ViewState. ViewState is built-in into Web Forms pages and automatically retains control values between multiple requests for the same page.

ViewState is implemented through a hidden form field control called __VIEWSTATE. When a Web page is about to be submitted back to the server it takes a look at the state (value) in each control and writes the name of the control and its state into the __VIEWSTATE control. If you look at the source of an .aspx page and examine the __VIEWSTATE control, it will look something like this.

<input type="hidden" name="__VIEWSTATE" value="dDw3ODczMjU5O3Q8O2w8aTwwPjtpPDE+O2k8M j47aTwzPjs+O2w8dDxwPGw8aW5uZXJodG1sOz47bDxUY WtlTm90ZSBUZWNobm9sb2dpZXMgJmd0XDsgSG9tZTs+P js7Pjt0PHA8bDxpbm5lcmh0bWw7PjtssgYm9yZGVyLXJ pZ2h0OiAjNDA0MDQwIDBweCBzb2xpZFw7ICBiYWNz47d Dw7bDxpPDE+Oz47bDx0PHA8cDxsPFRleHQ7PjtsPFxlO z4+Oz47Oz47Pj47Pj47dDw7bDxpPDE+O2k8Mz47aTw1P jtpPDc+Oz47bDx0PHA8cDxsPE5hdmlnYXRlVXJsO ==" />

While this mess isn't easily decipherable to us (yes, it is decipherable), fortunately for us, the ASP.NET parser knows how to extract all of the controls, names, and values from this mess and .NET knows how to use the information from the parser to repopulate the controls.

Flipping ViewState On and Off
You may have guessed that parsing the ViewState information can involve some serious processing on the server for a well-populated page. Since ViewState isn't a priority for every page you build you can turn it off when you don't need to use it. Doing so will reduce the demand on server resources and increase the speed of your application.

There are two ways to enable or disable ViewState functionality: at the page level or for individual controls. To disable it at the page level set the enableViewState attribute to False in the Page directive at the top of the page.

<%@ Page enableViewState="False"%>

Similarly, set the EnableViewState property to False to disable ViewState on a specific control (see Figure 1.)

Figure 1. TextBox Control Property Sheet: You can find the enableSessionState property for a control on the control's property sheet.
Hitching a Ride on ViewState
There may be times when you will want to send other information to the server along with the current page so that same information package returns when the page completes its round trip. The ViewState() function accepts the name of the value to store as a parameter and then the value to save is assigned.

For example, suppose I want to track how many times the current page has been submitted by the current user. The Page_Load method code would look like this.

If Not Page.IsPostBack Then ViewState("PageCount") = 1 Else ViewState("PageCount") = _ CInt(ViewState("PageCount")) + 1 End If Me.lblPageCount.Text = _ ViewState("PageCount").ToString

You can create as many ViewState name/value pairs as you like.

Advantages of Using ViewState
Besides just being built in, ViewState provides some other advantages.

  • It is easy to implement.
  • It retains page and control state for you automatically.
  • It does not use server resources since all of the state information is contained within the page itself.
Disadvantages of Using ViewState
Because ViewState information is stored in the page code itself, the potential for ViewState bloat exists for heavily populated pages or pages containing DataGrids.

  • Performance problems with ViewState can cause a page to display slowly and cause a delay when it is posted back.
  • Page security can be a problem when using ViewState since the ViewState data is available in the page source and, even though it is encrypted, it can still be tampered with.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date