Home

ASP.NET Topics: Transferring Values From One (Form of a) Web Page to Another (Form of a Web Page)

     

Introduction

This example shows how to transfer values from controls of a form to a different web page.

 

ApplicationApplication: Implementing the Exercise

  1. Start your programming environment
  2. Create an ASP.NET Empty Web Site named transfervalues and click OK
  3. To create a new page, on the main menu, click Website -> Add New Item...
  4. Click Web Form and set the Name to result
  5. Click Add
  6. To create a new page, on the main menu, click Website -> Add New Item...
  7. Click Web Form and set the Name to employee
  8. Click Add
  9. Design the form as you see fit. Here is an example:
    <%@ Page Language="C#" AutoEventWireup="true"
        CodeFile="employee.aspx.cs" Inherits="employee" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>New Employee</title>
    </head>
    <body>
    
    <h2>New Employee</h2>
    
    <form id="frmEmployee" runat="server">
    <div>
    
    <table style="width:300px;"> 
      <tr>
        <td>Employee #:</td> 
        <td><asp:TextBox ID="txtEmployeeNumber" runat="server"></asp:TextBox></td> 
      </tr> 
      <tr> 
        <td>First Name:</td> 
        <td><asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td> 
      </tr> 
      <tr> 
        <td>Last Name:</td> 
        <td><asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td> 
      </tr>
      <tr> 
        <td>Title:</td> 
        <td><asp:TextBox ID="txtTitle" runat="server"></asp:TextBox></td> 
      </tr> 
      <tr> 
        <td> </td> 
        <td>
          <asp:Button ID="btnCreate" runat="server" Text="Create" Width="126px" />
        </td> 
      </tr>
    </table>
    
    </div>
    </form>
    </body>
    </html>

    Employee

  10. Click the button on the form and, using the Properties window, set the PostBackUrl field to the name of the other page: result.aspx
  11. Got to the other web page (result.aspx) and design it as you see fit. Here is an exammple:
    <%@ Page Language="C#"
             AutoEventWireup="true"
             CodeFile="result.aspx.cs"
             Inherits="result" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Employee Result</title>
    </head>
    <body>
    
    <h2>Employee Result</h2>
    
    <form id="frmResult" runat="server">
    <div>
    
    <table style="width:300px;"> 
      <tr> 
        <td>Employee #:</td> 
        <td>
          <asp:Label ID="lblEmployeeNumber" runat="server" Text="Label"></asp:Label> 
        </td> 
      </tr> 
      <tr> 
        <td>First Name:</td> 
        <td> 
          <asp:Label ID="lblFirstName" runat="server" Text="Label"></asp:Label> 
        </td> 
        </tr> 
        <tr> 
          <td>Last Name:</td> 
        <td> 
          <asp:Label ID="lblLastName" runat="server" Text="Label"></asp:Label> 
        </td> 
      </tr> 
      <tr> 
        <td>Title:</td> 
        <td> 
          <asp:Label ID="lblTitle" runat="server" Text="Label"></asp:Label> 
        </td> 
      </tr> 
    </table> 
        
    </div>
    </form>
    </body>
    </html>

    Employee Result

  12. In the Solution Explorer, expand result.aspx and double-click result.aspx.cs
  13. In the Load event, call the Request.Form[] indexed property for each control of the first form. Here is an example:
    Partial Class result
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object,
                                ByVal e As System.EventArgs) Handles Me.Load
            lblEmployeeNumber.Text = Request.Form("txtEmployeeNumber")
            lblFirstName.Text = Request.Form("txtFirstName")
            lblLastName.Text = Request.Form("txtLastName")
            lblTitle.Text = Request.Form("txtTitle")
        End Sub
    End Class
  14. To create a new web page, on the main menu, click Website -> Add New Item...
  15. Click HTML Page
  16. Set the Name to index
  17. Click Add
  18. Add a link to the page where the values will be entered. Here is an example:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Exercise</title>
    </head>
    <body>
    
    <h2>Exercise</h2>
    
    <a href="employee.aspx">New Employee</a>
    </body>
    </html>
    
  19. Execute the application and test it
     

    Employee

    Employee

    Employee

  

Home Copyright © 2010-2011 C# Key