Pass Variables Between Pages - (Cross Page Posting)

InterServer Web Hosting and VPS

Pass Variables Between Pages - (Cross Page Posting)

Here we'll look at one particular way of how to pass variables between pages, which is known as Cross Page Posting.

There are many ways to pass variable values between pages, for instance you could use QueryStrings, or you could use Session, you could make the form postback to a different page, store the variable in the Application or Cache object. There are related issues, such as Session may time out, QueryStrings are open to change by the user, the Application and Cache object are shared for all users and posting back to a different page has it's own issues. You could even store the value in a database, but this means extra database access time.

We're going to see how to use the PreviousPage property which is part of the process known as Cross Page Posting. This works by setting a property on the page, adding the PreviousPageType directive to your next page which will access the property, and calling Server.Transfer to load the next page. What this allows us to do is actually access the property from the first page, as a strongly typed property on the second page, almost as if it were a property on the second page. This is just fantastic and removes the hassle if you use any of the above named methods for passing variables between web pages, such as casting Session or parsing the QueryString. It does however have a drawback, and that is that URL of the page visible in the browser shows the first page, it doesn't update to show the new page URL. If you can live with this drawback, been able to access the variable as a property is a much nicer way to pass values around between different web forms.

Pass Variables Between Pages - (Cross Page Posting) - Example

Here we'll see an example of how to use the PreviousPage property of the Page to pass variables between pages in C#.

First of all, consider we have 2 pages, page 1 will pass a variable to page 2.

In page 1, set a property you want to pass to page 2, such as shown here:

public String LastPageValue
        return "Some string value";


Next, when we are ready to move to page 2, we use Server.Transfer, such as in the OnClick event of a button:


Server.Transfer works by continuing execution of code from the currently executed page to the new specified one whilst in the same request. This differs from Response.Redirect which would return to the client as a Redirect and then begin execution of the new page back on the server.

PreviousPageType Directive

On page 2, we need to specify the PreviousPageType directive on the ASPX page. This allows page 2 to reference properties set in page 1, through the PreviousPage property. We set the VirtualPath property to point to page 1.

<%@ PreviousPageType VirtualPath="~/page1.aspx" %>

PreviousPage property

Now in the code on page 2, we can use the PreviousPage property to access the property we set on page 1, like below:

String lastPageValue = "";
if (PreviousPage != null)
    lastPageValue = PreviousPage.LastPageValue;
    lastPageValue = "no value";

Sponsored Links
SolarSystem - AngularJS Demo
SolarSystem - Angular JS Demo

Check out my first Angular JS demo hosted in Azure, it helped win my first Angular contract. Put the sound on.