首先是PostBackUrl,像Button、LinkButton等控件都有这个属性。看下面的例子:
Demo.aspx
<asp:HiddenField ID="Hidden_ID" runat="server" Value="Test" />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="Test.aspx">LinkButton</asp:LinkButton>
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="Test.aspx" />
Test.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
HiddenField Hidden_ID = (HiddenField)PreviousPage.FindControl("Hidden_ID");
if (Hidden_ID != null)
{
Label1.Text = Hidden_ID.Value;
}
}
else
{
Label1.Text = "null";
}
}
Server.Transfer是一个基于服务器的方法,而PostBackUrl是基于客户端的。从这个意义上来讲,跨页面传送比Server.Transfer更加的便捷,有利于提高服务器的性能,减少了处理步骤。除开这一点,两者还是非常相似的,不过在ASP.NET 2.0中,跨页面传送显然是更好的选择。
如果要在后续页面中判断PerviousPage所引用的页面是通过跨页面提交而来的还是通过Server.Transfer方法传送而来的,可以通过IsCrossPagePostBack属性来进行判断。
if (PreviousPage != null)
{
if (PreviousPage.IsCrossPagePostBack == true)
{
Response.Write("Cross-page post.");
}
}
else
{
Response.Write("Not a cross-page post.");
}
一、用FindControl方法获取发送页的值:
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>
<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.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:
<!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");
}