1. Home
  2. Computing & Technology
  3. Visual Basic

Learn ASP.NET 2.0 - ASP.NET 2.0 Server Controls
Viewstate - The solution to a unique web problem

By , About.com Guide

Apr 13 2008

The interaction between the web client and server is a key part of ASP.NET programming. Another, however, is problem of maintaining "state." The problem is that web pages are inherently stateless but client/server applications require that state be maintained. But what is "state"?

The problem is that the standardized way that web servers work is to treat every request as a new request. But some information, such as the value of a number that is calculated from other numbers, has to be "remembered" somewhere. For example, suppose a web page counts the number of times a login is attempted for security. The number can't be saved in clear text by the client because it would be possible for hackers to alter it and defeat the security. The answer is called Viewstate in ASP.NET.

If you check View Source for an ASP.NET web page, you'll see a statement that looks similar to this:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJMTM4ODA1MjE5ZGQJ5apz6hIqa5IAxxezj37vz58SNw==" />

This value is posted back to the server and tells the server the value of variable data that needs to be tracked. You can see the Viewstate information in a debugging display available to you by including a new Trace="True" attribute in the Page directive of your aspx file:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Viewstate can be disabled for a whole page or just for one control. If EnableViewState is set to False for Label1, the following code will always set it to the value 1 (when the initial Text property is set to "0").

Label1.Text = (CInt(Label1.Text) + 1).ToString

Because the information that must be transferred between client and server using Viewstate can become large, it's sometimes disabled is to make pages load faster. For added flexibility, ASP.NET 2.0 now has a new Control State that can be used to store the state of a control, separate from the ViewState.

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Learn ASP.NET
  6. Learn ASP.NET 2.0 - ASP.NET 2.0 Server Controls

©2009 About.com, a part of The New York Times Company.

All rights reserved.