ASP.NET2.0 使用PostBackUrl属性实现跨页面传值

一、用FindControl方法获取发送页的值:

 

Page1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>

<!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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Page2.aspx" />
    
</div>
    
</form>
</body>
</html>

 

Page2.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class Page2 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        TextBox pp_Textbox1;
        Calendar pp_Calendar1;

        pp_Textbox1 
= (TextBox)PreviousPage.FindControl("Textbox1");
        pp_Calendar1 
= (Calendar)PreviousPage.FindControl("Calendar1");

        Response.Write(pp_Textbox1.Text 
+ " - " + pp_Calendar1.SelectedDate.ToShortDateString());
    }
}

 

二、从属性中获得控件的值:

Page1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>

<!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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" PostBackUrl="~/Page2.aspx" />
    
</div>
    
</form>
</body>
</html>

 

Page2.aspx

注意使用的是PreviousPageType指令:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>
<%@ PreviousPageType VirtualPath="~/Page1.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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
    
</div>
    
</form>
</body>
</html>

 

Page2.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class Page2 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(PreviousPage.pp_TextBox1.Text 
+ " - " + PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString());
    }
}

 

如果在未处理Page1.aspx之前。有人请求了Page2.aspx,该怎么办?使用PreviousPage.IsCrossPagePostBack属性。注意:须先判断PreviousPage是否为空,否则出报错:未将对象的引用设置到对象的实例

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class Page2 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (PreviousPage != null)
        {
            
if (PreviousPage.IsCrossPagePostBack)
            {
                Response.Write(PreviousPage.pp_TextBox1.Text 
+ " - " + PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString());
            }
            
else
            {
                Response.Redirect(
"Page1.aspx");
            }
        }
    }
}

 

 

posted on 2008-08-09 10:51  王丹小筑  阅读(1651)  评论(0)    收藏  举报

导航