1. Technology

Using View State in ASP.NET Web Pages

View State is the automatic way to maintain state information.


Updated October 16, 2010

Hopefully, you reached this page from the About Visual Basic ASP.NET Hub Page. If not, you might want to click the link above and go there.

The entire web is inherently "stateless" because it's a fundamental part of the "http protocol" which is the way the entire web works. (Click here for an overview of state in ASP.NET including links to articles describing other ways to maintain state.) There are several techniques to maintain state in an ASP.NET application including view state. This article is an overview about how view state works.

According to Microsoft, view state is a way to "persist changes to the state of a Web Form across postbacks". What does that mean? Let's dig deeper.

On the surface, the concept is simple. If you have a web form with, for example, a TextBox on it, then the current properties of that TextBox (such as the Text property) must be available to your ASP.NET application as long as the application is running. Remember that the form and and the TextBox are actually in memory on the server. What you see on your browser at the client is just a collection of HTML that looks like a form with a TextBox; there are no actual objects there. After the server sends this HTML to your browser, the server drops the connection and deletes your application from memory.

View state saves the information in a hidden form field on the web page. That way, it's returned to the server with the next postback so the server can re-establish the information in server memory.

This is a really subtle point, but view state doesn't persist all of the properties of everything on the form. ASP.NET is more efficient than that. As Scott Mitchell puts it in an MSDN article, "It is a common misconception among developers that view state is somehow responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web controls remember their values across postback. This is not the case, as the values are identified via posted back form field values." Control properties that are assigned in the .aspx code won't be in the view state.

But there are changes to form controls that do need to be persisted. It all happens automatically. You don't have to do a thing because the code to make it happen is a fundamental part of ASP.NET. When you add that TextBox (or other ASP.NET control) to your web page, view state is automatically in force.

ASP.NET does this by creating a hidden form field called __VIEWSTATE and stashing state changes for all of the controls in that field. You can see ViewState with the simplest of web pages in the illustration below. Remember that properties for more than just the Text property of the TextBox can be in view state.

Click Here to display the illustration

You can see that the ViewState is not a simple text field. In fact, it's serialized into a base-64 encoded string for efficiency. (Not for security as I have even seen in some books because decoding the string again is easy.)

View state can be used for more than just the changed properties of controls. You can use it in your program code too. The syntax is a simple key / value pair. For example if you want to persist the string "My Funky String", you can do it with a statement like this:

ViewState("theKey") = "My Funky String"

The process and the running web page is shown in the illustration.

Click Here to display the illustration

There's a price for view state, however. Especially on complicated web pages with lots of controls, the view state hidden field can grow to a substantial size. Remember that this gets sent back to the web server with every postback. It can start to really affect performance.

Before ASP.NET 4.0, you could turn view state off completely for a page, but anything else wasn't possible. One of the big improvements in ASP.NET 4.0 is the ability to selectively turn off view state for each control. The illustration shows the ViewStateMode property for a Button control.

Click Here to display the illustration

As you can see, there are some ways to manage view state, but usually it's one of those features of ASP.NET that "just works".

  1. About.com
  2. Technology
  3. Visual Basic
  5. ASP.NET View State

©2014 About.com. All rights reserved.