Passing Server Control Values Between Pages

When creating a Web Forms application, it is often necessary to pass information from one Web Forms page to another. This allows information to be entered on one Web Forms page and then submitted to another page for processing.

The design pattern for passing values between Web Forms pages using code inline is slightly different from that for code-behind files. Which design pattern you choose depends on your preference for using inline code or code-behind files. Both design patterns are discussed in the following sections.

Design Pattern Using Inline Code

When passing values to another Web Forms page using code inline, you first need to specify a class name for the Web Forms page that contains the information you want to send. Specify a class name for the Web Forms page by including the ClassName attribute together with the name of the class in the @ Page directive. Next, create a property with a get accessor, in the class, for each value you want to share. The get accessor should return the value you want to pass, such as the value of a text box. To send the information, transfer control of the application to a different Web Forms page by using the Transfer method of the Server object.

On the receiving Web Forms page, reference the class declared in the sending page by adding an @ Reference directive at the top of the page with the Page attribute set to the sending page. The receiving Web Forms page can then access the information by first retrieving an instance of the handler that first received the HTTP request from the Handler property of the HttpContext object. The handler object is then converted to an instance of the class encapsulating the passed information. Once the conversion is performed, the passed values can be accessed through the properties of the converted object.

Caution   Do not pass sensitive information (such as credit card information or passwords) between pages.

To create a Web Forms page that sends values to another Web Forms page

  1. Specify a class name for the source Web Forms page by adding an @ Page directive at the top of the Web Forms page, with the ClassName attribute set to a valid class name.

    <%@ Page Language="VB" ClassName="MyClassName" %>
    [C#]
    <%@ Page Language="C#" ClassName="MyClassName" %>
    
  2. Define a property with a get accessor, in the class, for each value you want to pass to another Web Forms page. The get accessor should simply return the value you want to pass, such as the value of a text box on the Web Forms page. The properties must be defined in server-side script.

    <script runat="server">
       Public ReadOnly Property FirstName() As String
          Get
             ' FirstNameTextBox is the name of a TextBox control.
             Return FirstNameTextBox.Text
          End Get
       End Property
    </script>
    [C#]
    <script runat="server">
       public string FirstName
       {
          get
          {
             // FirstNameTextBox is the name of a TextBox control.
             return FirstNameTextBox.Text;
          }
       }
    </script> 
    
  3. When you want to pass the information to another Web Forms page, such as when a button is clicked, use the HttpServerUtility.Transfer method to end execution on the current page and transfer control of the application to another Web Forms page. The HttpServerUtility.Transfer method takes a single parameter that allows you to specify the URL of the Web Forms page you want to transfer control to.

    Sub SubmitButtonClick(sender As Object, e As EventArgs)
       Server.Transfer("secondpage.aspx")
    End Sub
    
    [C#]
    void SubmitButtonClick(object sender, EventArgs e)
    {
       Server.Transfer("secondpage.aspx");
    }
    

The following shows a complete example of how to create a Web Forms page, using inline code, to pass the values of two TextBox controls to another Web Forms page. This sample must be called Firstpage.aspx.

<%@ Page Language="VB" ClassName="FirstPageClass" %>

<html>
<head>
 
   <script runat="server">
      Public ReadOnly Property FirstName() As String
         Get
            ' first is the name of a TextBox control.
            Return first.Text
         End Get
      End Property

      Public ReadOnly Property LastName() As String
         Get
            ' last is the name of a TextBox control.
            Return last.Text
         End Get
      End Property

      Sub ButtonClicked(sender As Object, e As EventArgs) 
         Server.Transfer("secondpage.aspx") 
      End Sub

   </script> 

</head>

<body>

   <form runat="server">
      First Name: 
      <asp:TextBox id="first" 
           runat="server"/> 
      <br>
      Last Name: 
      <asp:TextBox id="last" 
           runat="server"/>
      <br>
      <asp:Button 
           OnClick="ButtonClicked" 
           Text="Go to second page"
           runat=server />
   </form>

</body>

</html>
[C#]
<%@ Page Language="C#" ClassName="FirstPageClass" %>

<html>
<head>
 
   <script runat="server">

      public string FirstName 
      { 
         get 
         { 
            return first.Text; 
         } 
      }

      public string LastName 
      { 
         get 
         { 
            return last.Text; 
         } 
      }

      void ButtonClicked(object sender, EventArgs e) 
      { 
         Server.Transfer("secondpage.aspx"); 
      }

   </script> 

</head>

<body>

   <form runat="server">
      First Name: 
      <asp:TextBox id="first" 
           runat="server"/> 
      <br>
      Last Name: 
      <asp:TextBox id="last" 
           runat="server"/>
      <br>
      <asp:Button 
           OnClick="ButtonClicked" 
           Text="Go to second page"
           runat=server />
   </form>

</body>

</html>

To create a Web Forms page that receives values from another Web Forms page

  1. On the Web Forms page that receives the information, add an @ Reference directive at the top of the page, with the Page attribute set to the source Web Forms page (the Web Forms page that contains the information you want to pass).

    <%@ Reference Page="firstpage.aspx" %>
    
  2. Declare a variable in server-side script to store an instance of the class defined in the Web Forms page sending the information.

    <script runat="server">
       Dim fp As FirstPageClass
    </script>
    [C#]
    <script runat="server">
       FirstPageClass fp;
    </script>
    
  3. Create a custom Page_Load event handler that assigns the IHttpHandler implemented object for the current HTTP request to the variable declared in the previous step when the Web Forms page does not post back to itself. Use the IsPostBack property to determine whether the page is posting back to itself. The IHttpHandler implemented object contains an instance of the handler that first received the HTTP request. Because the IHttpHandler implemented object is not the same type of object as the variable declared in the previous step, it must be converted to the class encapsulating the information sent from the first Web Forms page before it can be assigned to the variable. Retrieve the handler object by using the Handler property of the HttpContext object.

    <script runat="server">
       Sub Page_Load()
          If Not IsPostBack Then
             fp =  CType(Context.Handler, FirstPageClass)
          End If
       End Sub
    </script>
    
    [C#]
    <script runat="server">
       void Page_Load()
       {
          if (!IsPostBack)
          {
             fp = (FirstPageClass)Context.Handler;
          }
       }
    </script>
    
  4. The variable declared in the second step now contains an instance of the class encapsulating the information from the previous Web Forms page. Use the variable to access the properties of the class that contain the information sent from the previous Web Forms page. You can programmatically access these values to perform a calculation, or simply display them using the script delimiters <%= and %>.

    Hello <%=fp.FirstName%>
    

The following shows a complete Web Forms page that receives two values from another Web Forms page. The values are then displayed on the Web Forms page. You must call this sample Secondpage.aspx.

<%@ Page Language="VB" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>
 
   <script runat="server">

      Dim fp As FirstPageClass

      Sub Page_Load() 
         If Not IsPostBack Then
            fp = CType(Context.Handler, FirstPageClass)
         End If 
      End Sub

   </script>

</head> 

<body>

   <form runat="server">

      Hello <%=fp.FirstName%> <%=fp.LastName%>

   </form>

</body>

</html>

[C#]
<%@ Page Language="C#" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>
 
   <script runat="server">

      FirstPageClass fp;

      void Page_Load()
      {
         if (!IsPostBack)
         {
            fp = (FirstPageClass)Context.Handler;
         }
      }
   
   </script>

</head> 

<body>

   <form runat="server">

      Hello <%=fp.FirstName%> <%=fp.LastName%>

   </form>

</body>
</html>

Design Pattern Using Code-Behind Files

When using code-behind files, the code-behind file contains a class declaration for the code associated with the Web Forms page. In the code-behind file for the Web Forms page sending the information, first create a property with a get accessor in the class for each value you want to share. The get accessor should return the value you want to pass, such as the value of a text box. To send the information, transfer control of the application to a different Web Forms page by using the Transfer method of the Server object.

On the receiving Web Forms page, reference the class declared in the sending page by adding an @ Reference directive, at the top of the page, with the Page attribute set to the sending page. You can then retrieve an instance of the handler that first received the HTTP request from the Handler property of the HttpContext object.

Note   To make the class declared in the sending Web Forms page available in the code-behind file of the receiving Web Forms page, you must manually compile the code-behind files of each Web Forms page, using the command line compiler, into a single .dll file. The .dll file must be placed in the \Bin directory of the Web Forms application.

The handler object is then converted to an instance of the class encapsulating the passed information. Once the conversion is performed, the passed values can be accessed through the properties of the converted object.

Caution   Do not pass sensitive information (such as credit card information or passwords) between pages.

To send Server control values from a different Web Forms page

  1. Create a code-behind file for the sending Web Forms page that contains a class declaration for the code associated with the page.

    Imports System
    ' Add other references here.
    
    Public Class FirstPageClass
       Inherits System.Web.UI.Page
    
       ' Add class code here.
    
    End Class
    [C#]
    Imports System
    // Add other references here.
    
    public class FirstPageClass : System.Web.UI.Page
    {   
       // Add class code here.
    }
    
  2. To access the server controls on the Web Forms page in the class declared in the code-behind file, declare protected variables in the class to represent the server controls you want to access.

    Protected FirstNameTextBox As System.Web.UI.WebControls.TextBox 
    [C#]
    protected System.Web.UI.WebControls.TextBox FirstNameTextBox;
    
  3. In the class declared in the first step, define a property with a get accessor for each value you want to pass to another Web Forms page. The get accessor should simply return the value you want to pass, such as the value of a text box on the Web Forms page.

    Public ReadOnly Property FirstName() As String
       Get
          ' FirstNameTextBox is the name of a TextBox control.
          Return FirstNameTextBox.Text
       End Get
    End Property
    [C#]
    public string FirstName
    {
       get
       {
          // FirstNameTextBox is the name of a TextBox control.
          return FirstNameTextBox.Text;
       }
    }
    
  4. When you want to pass the information to a different Web Forms page, such as when a button is clicked, use the HttpServerUtility.Transfer method to end execution on the current page and transfer control of the application to another Web Forms page. The HttpServerUtility.Transfer method takes a single argument that allows you to specify the URL of the Web Forms page you want to transfer control to.

    Sub SubmitButtonClick(sender As Object, e As EventArgs)
       Server.Transfer("secondpage.aspx")
    End Sub
    
    [C#]
    void SubmitButtonClick(object sender, EventArgs e)
    {
       Server.Transfer("secondpage.aspx");
    }
    
  5. Create the interface for the Web Forms page sending the information. Make sure to add an Inherits attribute to the @ Page directive that is set to the class declared in the code-behind file.

    <%@ Page Language="VB" Inherits="FirstPageClass" %>
    
    <html>
    <head>
    
    </head>
    
    <body>
    
       <!-- Add User Interface here -->
    
    </body>
    [C#]
    <%@ Page Language="C#" Inherits="FirstPageClass" %>
    
    <html>
    <head>
    
    </head>
    
    <body>
    
       <!-- Add User Interface here -->
    
    </body>
    

The following shows a complete example of the code-behind file associated with the Web Forms page sending the information. Depending on whether you use Visual Basic or C#, make sure this sample is called Firstpage.aspx.vb or Firstpage.aspx.cs, respectively.

Imports System

Public Class FirstPageClass :
   Inherits System.Web.UI.Page

   Protected first As System.Web.UI.WebControls.TextBox
   Protected last As System.Web.UI.WebControls.TextBox
   Protected Button1 As System.Web.UI.WebControls.Button

   Public ReadOnly Property FirstName() As String
      Get
         ' first is the name of a TextBox control.
         Return first.Text
      End Get
   End Property

   Public ReadOnly Property LastName() As String
      Get
         ' last is the name of a TextBox control.
         Return last.Text
      End Get
   End Property

   Sub ButtonClicked(sender As Object, e As EventArgs) 
      Server.Transfer("secondpage.aspx") 
   End Sub

End Class
[C#]
using System;

public class FirstPageClass : System.Web.UI.Page
{
   protected System.Web.UI.WebControls.TextBox first;
   protected System.Web.UI.WebControls.TextBox last;
   protected System.Web.UI.WebControls.Button Button1;

   public string FirstName 
   { 
      get 
      { 
         return first.Text; 
      } 
   }

   public string LastName 
   { 
      get 
      { 
         return last.Text; 
      } 
   }

   void ButtonClicked(object sender, EventArgs e) 
   { 
      Server.Transfer("secondpage.aspx"); 
   }

}

The following shows a complete example of how to create a Web Forms page with a code-behind file to pass the values of two TextBox controls to another Web Forms page. Make sure this sample is called Firstpage.aspx.

<%@ Page Language="VB" Inherits="FirstPageClass" %>

<html>
<head>

</head>

<body>

   <form runat="server">
      First Name: 
      <asp:TextBox id="first" 
           runat="server"/> 
      <br>
      Last Name: 
      <asp:TextBox id="last" 
           runat="server"/>
      <br>
      <asp:Button
           id="Button1" 
           OnClick="ButtonClicked" 
           Text="Go to second page"
           runat=server />
   </form>

</body>

</html>

[C#]
<%@ Page Language="C#" Inherits="FirstPageClass" %>

<html>
<head>

</head>

<body>

   <form runat="server">
      First Name: 
      <asp:TextBox id="first" 
           runat="server"/> 
      <br>
      Last Name: 
      <asp:TextBox id="last" 
           runat="server"/>
      <br>
      <asp:Button
           id="Button1" 
           OnClick="ButtonClicked" 
           Text="Go to second page"
           runat=server />
   </form>

</body>

</html>

To receive Server control values from a different Web Forms page

  1. Create a code-behind file, for the receiving Web Forms page, that contains a class declaration for the code associated with the page.

    Imports System
    ' Add other references here.
    
    Public Class SecondPageClass
       Inherits System.Web.UI.Page
    
       ' Add class code here.
    
    End Class
    [C#]
    Imports System
    // Add other references here.
    
    public class SecondPageClass : System.Web.UI.Page
    {   
       // Add class code here.
    }
    
  2. To access the server controls on the Web Forms page in the code-behind file, declare protected variables in the class to represent the server controls you want to access.

    Protected FirstNameTextBox As System.Web.UI.WebControls.TextBox 
    [C#]
    protected System.Web.UI.WebControls.TextBox FirstNameTextBox;
    
  3. Declare a variable in the class to store an instance of the class defined in the Web Forms page sending the information. If you want the variable available in the receiving Web Forms page, make it public.

    Note   The class defined in the Web Forms page sending the information cannot be accessed until you compile the code-behind files of each Web Forms page into a single .dll file. The .dll file must be placed in the \Bin directory of the Web application. This process is described in a later step.

    Public fp As FirstPageClass
    [C#]
    public FirstPageClass fp;
    
  4. Create a custom Page_Load event handler that assigns the IHttpHandler implemented object for the current HTTP request to the variable declared in the previous step (when the Web Forms page does not post back to itself). Use the IsPostBack property to determine whether the page is posting back to itself. The IHttpHandler implemented object contains an instance of the handler that first received the HTTP request. Because the IHttpHandler implemented object is not the same type of object as the variable declared in the previous step, it must be converted to the class encapsulating the information sent from the first Web Forms page before it can be assigned to the variable. Retrieve the handler object by using the Handler property of the HttpContext object.

    Sub Page_Load()
       If Not IsPostBack Then
          fp =  CType(Context.Handler, FirstPageClass)
       End If
    End Sub
    [C#]
    void Page_Load()
    {
       if (!IsPostBack)
       {
          fp = (FirstPageClass)Context.Handler;
       }
    }
    
  5. The variable declared in the third step now contains an instance of the class encapsulating the information from the previous Web Forms page. Use the variable to access the information sent from the previous Web Forms page. You can programmatically access these values to perform a calculation, or simply display them using the script delimiters <%= and %>.

    Hello <%=fp.FirstName%>
    
  6. Create the interface for the Web Forms page sending the information. In the @ Page directive, make sure to add an Inherits attribute that is set to the class declared in the code-behind file.

    <%@ Page Language="VB" Inherits="SecondPageClass" %>
    
    <html>
    <head>
    
    </head>
    
    <body>
    
       <!-- Add User Interface here -->
    
    </body>
    [C#]
    <%@ Page Language="C#" Inherits="SecondPageClass" %>
    
    <html>
    <head>
    
    </head>
    
    <body>
    
       <!-- Add User Interface here -->
    
    </body>
    
  7. Add an @ Reference directive at the top of the Web Forms page, receiving the information, with the Page attribute set to the source Web Forms page (the Web Forms page that contains the information you want to pass).

    <%@ Reference Page="firstpage.aspx" %>
    
  8. The sending and receiving Web Forms pages are both complete at this point. However, before the application can run properly, the code-behind files of each page must be compiled together into a single .dll file. This .dll file must be placed in the \Bin directory of the Web application. This allows the class declared in the sending Web Forms page to be accessible in the receiving Web Forms page. To compile the code-behind files, enter the following command in the command line:

    vbc /out:Bin\appname.dll /r:System.dll /r:System.Web.dll /t:library firstpage.aspx.vb secondpage.aspx.vb 
    [C#]
    csc /out:Bin\appname.dll /r:System.dll /r:System.Web.dll /t:library firstpage.aspx.cs secondpage.aspx.cs
    

The following shows a complete example of the code-behind file associated with the Web Forms page receiving the information. Depending on whether you use Visual Basic or C#, make sure this sample is called Secondpage.aspx.vb or Secondpage.aspx.cs, respectively.

Imports System

Public Class SecondPageClass  
   Inherits System.Web.UI.Page

   Protected DisplayLabel As System.Web.UI.WebControls.Label
   Public fp As FirstPageClass

Sub Page_Load()
   If Not IsPostBack Then
      fp =  CType(Context.Handler, FirstPageClass)
   End If
   End Sub

End Class
[C#]
using System;

public class SecondPageClass : System.Web.UI.Page
{

   protected System.Web.UI.WebControls.Label DisplayLabel;
   public FirstPageClass fp;

   void Page_Load() 
   {
      if (!IsPostBack)
      {
         fp = (FirstPageClass) Context.Handler;
      } 
   }

}

The following shows a complete example of how to create a Web Forms page with a code-behind file to receive the values passed from a different Web Forms page. Make sure this sample is called Secondpage.aspx

<%@ Page Language="VB" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>

</head> 

<body>

   <form runat="server">

      Hello <%=fp.FirstName%> <%=fp.LastName%>

   </form>

</body>

</html>
[C#]
<%@ Page Language="C#" Inherits="SecondPageClass" %>
<%@ Reference Page="firstpage.aspx" %>

<html>

<head>

</head> 

<body>

   <form runat="server">

      Hello <%=fp.FirstName%> <%=fp.LastName%>

   </form>

</body>

</html>

See Also

@Page directive | HttpServerUtility.Transfer | TextBox | IHttpHandler | Handler