今天在CSDN上看到这个问题,特写了个解决方案,供参考.
问题描述:在Default2.aspx页面中点击按钮将TextBox1控件的值传递到Default3.aspx页面中去,供Default3.aspx页面使用。
关键点有2点:
1、Server.Transfer("Default3.aspx", true); //方法第二个参数为true
2、对于跳转到的页面的头部要添加跳转页面类得引用,<%@ Reference Page="Default2.aspx"%>,如果Default2.aspx在某个文件夹下的话,如Admin文件夹下的话,得这样写:<%@ Reference Page="Admin_Default2.aspx"%>(即是该页面类后台cs文件的完整类名,这个得尤其注意)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string name
{
get
{
return TextBox1.Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("Default3.aspx", true);
}
}
==========================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<%@ Reference Page="Default2.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>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strTxt = "";
Default2 oForm = (Default2)this.Context.Handler;
strTxt += "Default2页面中TextBox1文本框中的值为:" + oForm.name + "<br>";
Response.Write(strTxt);
}
}
}