使用PostBackUrl属性实现跨页面传值

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

Page1.aspx

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>Untitled Page</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
13 <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
14 <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Page2.aspx" />
15 </div>
16 </form>
17 </body>
18 </html>

Page2.aspx.cs:

 1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.HtmlControls;
8 using System.Web.UI.WebControls;
9
10
11 public partial class Page2 : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 TextBox pp_Textbox1;
16 Calendar pp_Calendar1;
17
18 pp_Textbox1 = (TextBox)PreviousPage.FindControl("Textbox1");
19 pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
20
21 Response.Write(pp_Textbox1.Text + " - " + pp_Calendar1.SelectedDate.ToShortDateString());
22 }
23 }

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

Page1.aspx

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>Untitled Page</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
13 <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
14 <asp:Button ID="Button1" runat="server" Text="Button" οnclick="Button1_Click" PostBackUrl="~/Page2.aspx" />
15 </div>
16 </form>
17 </body>
18 </html>

Page2.aspx

注意使用的是PreviousPageType指令:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>
2 <%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml">
7 <head runat="server">
8 <title>Untitled Page</title>
9 </head>
10 <body>
11 <form id="form1" runat="server">
12 <div>
13
14 </div>
15 </form>
16 </body>
17 </html>

Page2.aspx.cs

 1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.HtmlControls;
8 using System.Web.UI.WebControls;
9
10
11 public partial class Page2 : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 Response.Write(PreviousPage.pp_TextBox1.Text + " - " + PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString());
16 }
17 }

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

 1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Web;
6 using System.Web.UI;
7 using System.Web.UI.HtmlControls;
8 using System.Web.UI.WebControls;
9
10
11 public partial class Page2 : System.Web.UI.Page
12 {
13 protected void Page_Load(object sender, EventArgs e)
14 {
15 if (PreviousPage != null)
16 {
17 if (PreviousPage.IsCrossPagePostBack)
18 {
19 Response.Write(PreviousPage.pp_TextBox1.Text + " - " + PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString());
20 }
21 else
22 {
23 Response.Redirect("Page1.aspx");
24 }
25 }
26 }
27 }










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值