ASP.NET session variables, cookies, query string parameters, etc.
Part five of a five-part series of blogs

There is a range of techniques for remembering information in ASP.NET across pages and even sessions, including session variables, cookies, query string parameters and crosspage postback. This blog explains how to use each of these techniques, together with the pros and cons of each.

  1. Remembering State between Pages
  2. Passing Data by Query String in the URL
  3. Passing Data using Session Variables
  4. Using Cookies to Remember Things
  5. Cross-Page Postback (this blog)

This page is part of an online tutorial in ASP.NET.  Wise Owl's main business is running classroom-based training courses - have a look at our other .NET courses.

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.

Cross-Page Postback

Where one page always links to another (as in our example for this blog), you can use cross page postback to pass values, but there are a number of hoops to pass through.

Step 1 - Telling a Button to Post to Another Page

The first thing to do to get cross page postback to work is to assign a URL to the search button:

Postback URL set for button

When you click on the search button, it will post back to the page specified.

Note that if you set a PostBackUrl property for a button any code attached to its Click event will not run.

Step 2 - Writing Properties to Make Form Fields Easier to Access

Within the search form code, you should now create properties to allow another page to get at the search criteria entered.  In C#:

#region Cross page postback method

public string ContainsText {


get {

return txtContains.Text;


set {

txtContains.Text = value;




public string Status {

get {

return txtStatus.Text;


set {

txtStatus.Text = value;



Or in VB:

#Region "Cross page postback method"

ReadOnly Property Contains As String


Return txtContains.Text

End Get

End Property

ReadOnly Property Status As String


Return txtStatus.Text

End Get

End Property

#End Region

Step 3 - Telling the Second Page to Look for the First One

The third step to getting cross page postback to work is to add a directive to the target page, telling it which is its previous page:

The PreviousPageType page directive

The directive for VB (the C# directive would refer to frmTaskSearchCs.aspx instead).

Step 4 - Detecting Values from Previous Page Form Fields

Finally, you now need to add code to the to-do list page to detect the search form values typed in on the previous page.  The best place to do this is when you first load the page, whether in C#:

protected void Page_Load(object sender, EventArgs e)


if (!Page.IsPostBack) {


// first check for crosspage postback

if (PreviousPage != null) {


if (PreviousPage.IsCrossPagePostBack) {


litContains.Text = PreviousPage.ContainsText;

litStatus.Text = PreviousPage.Status;




Or in VB:

Protected Sub Page_Load(sender As Object,

e As System.EventArgs) Handles Me.Load


If Not Page.IsPostBack Then


'first check for crosspage postback

If PreviousPage IsNot Nothing Then


If PreviousPage.IsCrossPagePostBack Then


litContains.Text = PreviousPage.Contains

litStatus.Text = PreviousPage.Status

Exit Sub


End If


End If

What these code excerpts are saying is that:

  • if this is the first time the page has been loaded; and
  • if the previous page can be determined; and
  • if the previous page used cross page postback

then we can get at the previous page's properties easily.

Simplifying the Process using FindControl

You can miss out steps 2 and 3, but it makes it harder to get at information typed onto the first form (I've only shown the VB code this time, as I wouldn't recommend this method):

'get at the text entered on previous page the hard way

litContains.Text =

CType(PreviousPage.FindControl("txtContains"), TextBox).Text


'contrast with the easy way, using the defined property

litStatus.Text = PreviousPage.Status

I don't like crosspage postback.  It's messy to set up, and it relies on page B always getting its information from page A.  What happens if there is an alternative route to page B, coming directly from page C?

This blog has 0 threads Add post