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

本文介绍如何在ASP.NET的两个页面(test1.aspx和test2.aspx)之间传递文本框的值。通过设置按钮的PostBackUrl属性实现跳转,并在test2.aspx页面中使用PreviousPage属性获取前一页的控件值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

比如从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/archive/2012/07/10/2585032.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值