日期:2012-08-26  浏览次数:20439 次

有不少文章讨论怎么在两个页面间传递引用,如果你页面上的对象设置为public的话,你就可以传递他们在页面间。在页面间传递是使用
Server.Transfer替代Response.Redirect就可以。
例子:

-------------In Page A codebehind:

public class PageA : System.Web.UI.Page
{
public System.Web.UI.WebControls.TextBox TextBox1;
public System.Web.UI.WebControls.Button Button1;

// standard page code (Page_Load, etc)
// ....
// ....

private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("b.aspx");
}
}


-------------In Page B codebehind:


private void Page_Load(object sender, System.EventArgs e)
{
PageA myAPage = Context.Handler as PageA;
string textboxFromPageA = myAPage.TextBox1.Text;
}

因为当使用Server.Transfer时所有的对象(A and B)在服务器上当时都是活动的,你可以引用任何东西。

稍微修改一下b.aspx(uestc95 提供):

Page myPage =(Page) Context.Handler;
string textboxFromPageA;
textboxFromPageA = ((TextBox)myPage.FindControl("TextBox1")).Text;


这样在A.aspx中就可以正常的使用protected类型的了。

Good Luck!