Using ViewState to remember form values in ASP.NET
Part two of a two-part series of blogs

When you submit an ASP.NET form, how can you remember what was on it? Answer: using the ViewState bag.

  1. Maintaining State on a Web Page - Introduction
  2. The ViewState Bag (this blog)

This blog is part of a much longer tutorial on using ASP.NET.  We also run training courses in ASP.NET, using either Visual Basic or C#.

Posted by Andy Brown on 16 October 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

The ViewState Bag

ASP.NET provides a mechanism for remembering information about a page called ViewState.  This page looks at how it's held, and two ways to use it.

How is ViewState held?

The previous page contained a very simple form:

Simple page with 3 controls

A simple page showing the number of page views.


However, even for a simple page like this the generated HTML contains something a hidden variable caleld _VIEWSTATE:

Long ViewState value

The ViewState for any form contained in a hidden variable.

ViewState can get quite long!  I've just run a sample page in our internal Intranet, and it contains 6,956 characters in the viewstate variable.

Using the ViewState Bag

To remember anything on a form, you just add it to something called the ViewState bag, and it will be encoded and remembered in the _VIEWSTATE variable for the page somehow (you absolutely don't need to know how!).  Here's how to do this:

ViewState("AnyNameYouLike") = Anything you like

The ViewState bag can contain almost anything you like (including objects), although in our example it will be just an integer, whether in VB:

Protected Sub Page_Load(sender As Object, _

e As System.EventArgs) Handles Me.Load


Dim PageNumber As Integer

If Not Page.IsPostBack Then


'on first page load, set the page number to 1

PageNumber = 1

ViewState("PageNumber") = 1




'add 1 to page number

PageNumber = Convert.ToInt16(ViewState("PageNumber")) + 1


End If


'store and display new page number

ViewState("PageNumber") = PageNumber

litPageNumber.Text = PageNumber.ToString


End Sub

Or C#:

protected void Page_Load(object sender, EventArgs e)


int PageNumber;

if (!Page.IsPostBack) {


// on first page load, set the page number to 1

PageNumber = 1;

ViewState["PageNumber"] = 1;


} else {


// add 1 to page number

PageNumber = Convert.ToInt16(ViewState["PageNumber"]) + 1;




// store and display new page number

ViewState["PageNumber"] = PageNumber;

litPageNumber.Text = PageNumber.ToString();



Now the example from the previous part of this blog will work properly:

Number of pages showing OK

Each time you reload the page, the number of pages goes up by 1.


Note how you always have to convert information on its way into or out from the viewstate bag from the default type (object) to the one you want (integer in the above example).

Disadvantages of the ViewState Bag

The method shown above has three disadvantages:

Problem Notes
Case-sensitivity ViewState("SourceForm") and ViewState("sourceform") are not the same thing!  If you're a C# programmer this won't bother you, but it will if you code in VB.
Not strongly-typed When you type ViewState("SourceForm"). into the code window, no whizzy Intellisense will suggest ways to proceed.
Holds objects As mentioned in the hint above, the viewstate bag can hold anything, so you have to be careful to put the right things in, and convert them when you take them back out into the right data type.

Using Properties and Hidden Controls for ViewState

Wise Owl use hidden controls in HTML view and properties in code view to get round the 3 problems shown above.  Let's start with the hidden controls:

Hidden control for source form

The example uses this hidden control to remember which form we've just come from.


You can now create a read/write property to allow code to set and retrieve values for this viewstate variable, whether in VB:

Private Property SourceForm As String


'return value of hidden viewstate variable

Return litSourceForm.Text

End Get

Set(ByVal value As String)

'set value of hidden viewstate variable

litSourceForm.Text = value

End Set

End Property

Or in C#:

private string SourceForm




// retrieve value of hidden control

return litSourceForm.Text;




// set value of hidden control

litSourceForm.Text = value;



All of this seems like a lot of work, but the payoff comes when you try to use the SourceForm property.  We've managed to give the page a property which it will remember.  Here's an example of how you could set this either in VB:

'need to say where to go back to on cancel

SourceForm = "search"

Or in C#:

// need to say where to go back to on cancel

SourceForm = "search";

Why won't Microsoft merge these wretched languages?  When you want to retrieve the value of the property, it's just as easy, whether in VB:

'go back to form whence came

Select Case SourceForm

Case "search"

pnlTaskSearch.Visible = True

Case Else

pnlTaskList.Visible = True

End Select

Or C#:

// go back to form whence came

switch (SourceForm) {

case "search":

pnlTaskSearch.Visible = true;



pnlTaskList.Visible = true;


In our websites most pages begin with 2 or 3 hidden viewstate controls, usually to remember things like the id number of the current record being considered.

Suppressing ViewState

As a final note, it's worth mentioning that you can suppress viewstate for a control or for an entire page by setting the EnableViewState property to False.  For example:

EnableViewState property set to false

Here ViewState is disabled for a whole page.

You should be careful of the consequences of doing this: on the one hand, the HTML returned will be much shorter; on the other hand, you'll lose any memory for the page.

  1. Maintaining State on a Web Page - Introduction
  2. The ViewState Bag (this blog)
This blog has 0 threads Add post