现在的位置: 首页 > 综合 > 正文

跨页面回发(从page1发到page2)

2013年12月11日 ⁄ 综合 ⁄ 共 2019字 ⁄ 字号 评论关闭

page1 :

以下是page1页面的后台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page1.aspx.cs" Inherits="_Default" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        请输入内容:<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;<br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            PostBackUrl="~/page2.aspx" Text="提交到page2" />
    
    </div>
    </form>
</body>
</html>

以下是page1页面的前台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public string username
    {
        get { return this.TextBox1.Text;}
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
}

以下是page2页面的后台代码:

<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    
    </div>
    </form>
</body>
</html>

以下是page2页面的前台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class page2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage!=null)
        {
            //第一种方法(页内模式)
            TextBox txt = (TextBox)PreviousPage.FindControl("TextBox1");
            this.Label1.Text = txt.Text;

            //第二种方法(后台代码)
            this.Response.Write(this.PreviousPage.username);
        }
        else
        {
            this.Response.Redirect("page1.aspx");
        }
    }
}

抱歉!评论已关闭.