兩個aspx頁面間傳遞引用對象

有不少文章討論怎麼在兩個頁面間傳遞引用,如果你頁面上的對象設置爲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類型的了。

 

轉自:http://www.zzchn.com/edu/20070914/38507.shtml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章