导航

创建强类型化的PreviousPage 转

Posted on 2007-05-30 09:27  小西  阅读(948)  评论(2)    收藏  举报
1. 在app_code中添加c#文件 RegistrationInformation.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/**//// <summary>
/// RegistrationInformation 的摘要说明
/// </summary>

public struct RegistrationInformation
{
    
private string firstName;
    
public string FristName
    
{
        
get return firstName; }
        
set { firstName = value; }
    }


    
private string lastName;
    
public string LastName
    
{
        
get return lastName; }
        
set { lastName = value; }
    }


    
private string email;
    
public string Email
    
{
        
get return email; }
        
set { email = value; }
    }


    
private string selectedEvent;
    
public string SelectedEvent
    
{
        
get return selectedEvent; }
        
set { selectedEvent = value; }
    }


}

2. Defult.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>无标题页</title>
</head>
<body style="text-align: center">
    
<form id="form1" runat="server">
    
<div style="text-align: center">
        
<table>
            
<tr>
                
<td align="left" style="width: 100px">
                    
<asp:Label ID="lblEvent" runat="server" Text="Event:"></asp:Label>
                
</td>
                
<td align="left" style="width: 100px">
                    
<asp:DropDownList ID="dropDownListEvents" runat="server" Width="157px">
                        
<asp:ListItem>SQL 2005</asp:ListItem>
                        
<asp:ListItem>Office 2003</asp:ListItem>
                        
<asp:ListItem>ASP.NET</asp:ListItem>
                    
</asp:DropDownList></td>
            
</tr>
            
<tr>
                
<td align="left" style="width: 100px">
                    
<asp:Label ID="lblFirstName" runat="server" Text="FirstName:"></asp:Label></td>
                
<td align="left" style="width: 100px">
                    
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>&nbsp;
                
</td>
            
</tr>
            
<tr>
                
<td align="left" style="width: 100px">
                    
<asp:Label ID="lblLastName" runat="server" Text="LastName:"></asp:Label></td>
                
<td align="left" style="width: 100px">
                    
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td align="left" style="width: 100px">
                    
<asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label></td>
                
<td align="left" style="width: 100px">
                    
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td align="left" style="width: 100px">
                
</td>
                
<td align="left" style="width: 100px">
                    
<asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="~/ResultPage.aspx" OnClick="btnSubmit_Click" /></td>
            
</tr>
        
</table>
        
<br />
    
    
</div>
    
</form>
</body>
</html>
3.给default.aspx.cs添加属性RegistrationInformation
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{



    }

    
public RegistrationInformation RegistrationInformation
    
{
        
get
        
{
            RegistrationInformation ri 
= new RegistrationInformation();
            ri.FristName 
= txtFirstName.Text;
            ri.LastName 
= txtLastName.Text;
            ri.Email 
= txtEmail.Text;
            ri.SelectedEvent 
= dropDownListEvents.SelectedValue;
            
return ri;
        }


    }


}



4.在ResultPage.aspx中添加label空件,lblResult
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultPage.aspx.cs" Inherits="ResultPage" %>

<%@PreviousPageType VirtualPath="~/Default.aspx" %>

<!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>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div style="text-align: center">
        
<br />
        
<br />
        
<asp:Label ID="lblResult" runat="server"></asp:Label>&nbsp;</div>
    
</form>
</body>
</html>
5.在ResultPage.aspx.cs的Page_Load()方法中,添加如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ResultPage : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
try
        
{
            RegistrationInformation ri 
= PreviousPage.RegistrationInformation;

            lblResult.Text 
= ri.FristName + "  " + ri.LastName + "  selected the event: " + ri.SelectedEvent;

        }

        
catch
        
{
            lblResult.Text 
= "The originating page must contain txtFirstName,txtLastName,txtEmail controls";
        }

    }

}

文档来源 龙昱帅帅DotNet