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>
<!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());
}
}
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>
<!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>
<%@ 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());
}
}
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");
}
}
}
}
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");
}
}
}
}
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号