Navigate to Ken's Corner Home Page Frequently Asked ASP.NET Questions Classic Hits Game Written in ASP.NET Send Email to Ken Ken's Resume (available for contracts!) Microsoft Most Valuable Professional Award Ken's Corner of ASPAlliance

Passing Values from Aspx to Aspx

Although ASP.NET usually submits data to the current page for processing, there are times when you need to pass values from the controls on one page to controls or routines on another ASP.NET page.

This is a cookbook style tutorial that shows you how to pass from one page to another. For a deeper explanation, see the Microsoft Help file references at the bottom of the page.

  1. In Visual Studio.NET, start a new Visual Basic ASP.NET Web application as show in the following figure.

  1. Add a new Web form to the project, called one.aspx as shown in the following figure.

 

  1. In Design view of one.aspx, add two textboxes and a button.

  1. Double-click the button. In the default event handler subroutine, type the following code:

    Server.Transfer("two.aspx")

  1. Just above the Web Form Designer Generated Code, insert the following code, also as shown in the following figure.

Public ReadOnly Property FirstName() As String
 Get
  Return TextBox1.Text
 End Get
End Property

Public ReadOnly Property LastName() As String
 Get
  Return TextBox2.Text
 End Get
End Property

  1. Save your work and then create a new page called two.aspx as shown in the following figure:

  1. In the Design View of two.aspx, add a Label control as shown in the following figure.

  1. In a blank area of the Design View surface, double-click to create the default event handler for the page load.
  2. In the default handler, enter the following code, also shown in the following figure. (Don't worry about the syntax errors for the moment.)

    If Not IsPostBack Then
      fp = CType(Context.Handler, one)
      Label1.Text = fp.FirstName.ToString & _
      " " & fp.LastName.ToString
    End If

  1. Just above the Web Form Designer Generated Code, insert the following code, also as shown in the following figure.

    Public fp As one

  1. Save all of the files, and from the Build menu, click Build Solution.
  2. In Solution Explorer, select one.aspx and right-click. From the context menu, click View in Browser.
  3. Enter your first and last names in the text boxes and click the button, as shown in the following figure.

  1. Notice that the values you entered are now displayed in two.aspx.

For more information, view ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconpassingservercontrolvaluesbetweenpages.htm