ASP.NET Topics: Transferring Values From One (Form of a) Web Page to Another (Form of a Web Page)
This example shows how to transfer values from controls of a form to a different web page. Application: Implementing the Exercise
2 min read434 words
Introduction#
This example shows how to transfer values from controls of a form to a different web page. Application: Implementing the Exercise
- Start your programming environment
- Create an ASP.NET Empty Web Site named transfervalues and click OK
- To create a new page, on the main menu, click Website -> Add New Item...
- Click Web Form and set the Name to result
- Click Add
- To create a new page, on the main menu, click Website -> Add New Item...
- Click Web Form and set the Name to employee
- Click Add
- 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>
- Click the button on the form and, using the Properties window, set the PostBackUrl field to the name of the other page: result.aspx
- 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>
- In the Solution Explorer, expand result.aspx and double-click result.aspx.cs
- In the Load event, call the Request.Form[] indexed property for each control of the first form. Here is an example: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class result : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblEmployeeNumber.Text = Request.Form["txtEmployeeNumber"]; lblFirstName.Text = Request.Form["txtFirstName"]; lblLastName.Text = Request.Form["txtLastName"]; lblTitle.Text = Request.Form["txtTitle"]; } }
- To create a new web page, on the main menu, click Website -> Add New Item...
- Click HTML Page
- Set the Name to index
- Click Add
- 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>
- Execute the application and test it
Home Copyright © 2010-2011 C# Key

