Using ViewState to remember form values in ASP.NET
Part one 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 (this blog)
  2. The ViewState Bag

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.

Maintaining State on a Web Page - Introduction

Web pages don't maintain state - that is, they don't remember anything.

If you're a C++, VB6, Delphi or other traditional programmer you'll be wondering what the fuss is about; why not just declare public variables to hold the information?  The answer is that web pages don't retain information - as I'll prove later on down this page.

Our Problem - how to Remember Search and Task Information

Throughout this ASP.NET tutorial, we've been trying to build a simple system in the best possible way (to paraphrase the late Kenny Everett).  First, you should be able to search for to-do tasks:

Task search form List of tasks found
Search for tasks ... ... then show the results.

Having found a task, you should be able to see its details:

List of tasks Details of single task
Click on a task ... ... to see its details.

The 64-million-dollar question (should that be more, to allow for inflation?) is: how can we pass information between the 3 forms shown above?  That is:

  • how to pass the search criteria to the task list form?
  • how to pass the chosen task's id to the task details form?

A Multiple Page Solution

One answer would be to create three different pages:

Page What it would do
Search form Allow you to search for tasks.
Task list List out tasks meeting the search criteria.
Single task Allow you to view or edit the details for a single task.

You could use query string parameters, session variables, cookies or crosspage postback to pass information from one page to another, but you'd still have the problem of remembering what a form looked like:

Searching for tasks containing RE

When you have listed out tasks containing the letters RE, you want to be able to return to this form and see the same criteria still typed in.

 

There are a number of complicated solutions to the above multi-page problem, but none of them really works properly.  The basic problem is that each web page works best if it is self-contained: that is, we'll combine all 3 "pages" on one page.

However, for the sake of completeness I've also shown how to maintain state across pages in a separate blog, since it is usually an integral part of building a website.

Proving that Web Pages don't Remember State

It's worth taking a little digression just to prove that we have a problem - that web pages really don't remember state.  Consider the following page:

Form showing page number

A simple form showing the current number of page views.

 

This page contains a literal called litPageNumber, whose value is set to the number of page loads:

HTML for page number views

The HTML for the page above shows the litPageNumber control.

A Literal is just like a TextBox, except that it can't contain any formatting (it's just a raw bit of text).

Here's the code-behind for our web page - firstly in VB:

Partial Class frmPage

Inherits System.Web.UI.Page

 

Public PageNumber As Integer

Protected Sub Page_Load(sender As Object,

e As System.EventArgs) Handles Me.Load

 

If Not Page.IsPostBack Then

 

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

PageNumber = 1

 

Else

 

'add 1 to page number

PageNumber += 1

 

End If

 

'display new page number

litPageNumber.Text = PageNumber.ToString

 

End Sub

End Class

Here's the equivalent code for C#:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class frmPageC : System.Web.UI.Page

{

public int PageNumber;

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack) {

 

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

PageNumber = 1;

 

 } else {

 

// add 1 to page number

PageNumber += 1;

 

}

 

// display new page number

litPageNumber.Text = PageNumber.ToString();

}

}

It doesn't matter how many times you reload this page, it will always show a single page view:

Page showing 1 page view

The number of pages is always 1.

 

The reason for this behaviour is that public variables don't work like this for web pages.  Every time you reload a web page any previous properties are forgotten - so in our case, the value of the PageNumber variable is reset to 1 every time our page is loaded.

 

Now we've seen that pages don't easily remember state, how can we get them to ... remember state?   The answer is ViewState.

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