Visual Basic and Visual C# Concepts
Passing Values Between Web Forms Pages

There are many times when you will want to navigate from one page to another and pass values from the first page to the second. For example, you might have a page that prompts users for a name and password. When users submit the form, you want to call another page that authenticates the user.

There are a variety of ways to share information between pages:

  • Use a query string, which appends information onto the URL and passes it to the next page. This has the disadvantage of making the information visible. For details, see WebClient.QueryString Property and HttpRequest.QueryString Property.
  • Use Session state to store information that is then accessible globally to all pages in the user's current session. However, this takes server memory and the information is stored until the session expires, which can represent more overhead than you want when you are simply passing information to the next page. For details, see Web Forms State Management.
  • Use custom page properties and then redirect from the original page to the target page in a way that you can still read values in the old page. This strategy is described in this topic.

Web Forms pages are classes in your application, and as such, you can create properties for them as you would any class. However, Web Forms pages have a very short lifetime, since they effectively exist only long enough to perform page processing. Properties on a Web Forms page therefore have limited use, since they exist only while the page is being processed. But if you transfer control from one page to another using specific techniques, the second page can still access properties on the first page.

To create shareable values on the source page

  1. In code, declare one or more read-only properties on the page using standard syntax for properties. Return the property value you want to pass to the next page.

    The following example shows how you can declare a property called Property1 and sets its value to the value of text box on the page:

    ' Visual Basic
    Public ReadOnly Property Property1() As String
       Get
          Return TextBox1.Text
       End Get
    End Property
    
    // C#
    public string Property1
    {
       get
       {
          return TextBox1.Text;
       }
    }
  2. Call the next page by calling the Transfer method of the Server object (the HttpServerUtility class), passing it the URL of the page you want to pass information to.

    The following example shows how you might call a page called WebForm2 (in the same project) from an event handler:

    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Server.Transfer("Webform2.aspx")
        End Sub
    
    // C#
    private void Button1_Click(object sender, System.EventArgs e)
    {
       Server.Transfer("Webform2.aspx");
    }

To get the property values of the first page from the called page, create an instance variable of the source page class. You then assign to it the HTTP object (an instance of the IHttpHandler class), the object that received the original request.

To read property values from the source page in the called page

  1. Create a global instance variable that is typed to the class of the source page.

    The following example shows how to declare a variable called sourcepage that is of the type WebForm1:

    ' Visual Basic
    ' Put immediately after the Inherits statements
    ' at the top of the file
    Public sourcepage As WebForm1
    
    // C#
    // Put immediately after the opening brace of the class
    public class WebForm3 : System.Web.UI.Page
        {
            public WebForm1 sourcepage;
            // etc.
  2. In the Page_Load handler, get the source page from the Context.Handler object (IHttpHandler interface) and assign it to the variable that you created in step 1. You must cast the handler object to the type of the source page class.
    Note   You should only perform this logic the first time the page runs (that is, when the page is first called from the source page).
  3. Get the property values from the source page and use them as you would any object properties.
    Note   Be sure to save the property values (for example, in view state) if you want to use them in page processing other than the first page initialization. For details, see Introduction to Web Forms State Management.

    The complete Page_Load handler might look like this:

    ' Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       If Not Page.IsPostBack Then
          sourcepage = CType(Context.Handler, WebForm1)
          Label1.Text = sourcepage.Property1
       End If
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
       if (!IsPostBack){
          WebForm1 sourcepage = (WebForm1) Context.Handler;
          Label1.Text = sourcepage.Property1;
       }
    }

See Also

Web Forms State Management

Page view tracker