使用PreviousPage来获取前一页页面的元素

比如从test1.aspx页面点击按钮进入test2.aspx页面,如果想在test2.aspx页面中得到test1.aspx页面中某些控件的值.

test1.aspx前台代码,
这里要注意的按钮一定要设置postbackurl="test2.aspx" 属性,不能在它的CS代码中比如用Redirect 的方法

--> 1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="Print_test1" %>
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>无标题页</title>
8</head>
9<body>
10    <form id="form1" runat="server">
11    <div>
12        <asp:TextBox ID="TextBox1" runat="server" Text="初值:sadfasdf"></asp:TextBox>
13        <asp:Button ID="Button1" runat="server" Text="Button"
postbackurl="test2.aspx" /></div>
14    </form>
15</body>
16</html>
17test1.aspx.cs      //这个页面没写代码


Code
highlighting produced by Actipro CodeHighlighter
(freeware)
http://www.CodeHighlighter.com/

--> 1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class Print_test1 : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16    }
17}
18


test2.aspx
前台代码   //这里也没写什么东东

Code highlighting produced by Actipro CodeHighlighter
(freeware)
http://www.CodeHighlighter.com/

--> 1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="Print_test2" %>
2<%@ PreviousPageType VirtualPath="test1.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>无标题页</title>
9</head>
10<body>
11    <form id="form1" runat="server">
12    <div>
13   
14    </div>
15    </form>
16</body>
17</html>
18test2.aspx.cs


Code highlighting produced by Actipro CodeHighlighter
(freeware)
http://www.CodeHighlighter.com/

--> 1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class Print_test2 : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16        //Response.Write(this.PreviousPage.FindControl("TextBox1"));
17        string txt = ((TextBox)this.PreviousPage.FindControl("TextBox1")).Text;
18
19        Response.Write(txt);
20        //(this.PreviousPage).test();      
21    }
22}
23
在test2.aspx.cs
代码中使用类似((TextBox)this.PreviousPage.FindControl("TextBox1")).Text 的方法来访问前页的属性或方法.

原文地址:https://www.cnblogs.com/lyghost/p/2585032.html