两个aspx页面间传递引用对象。

来源:互联网 发布:大一c语言编程题库 编辑:程序博客网 时间:2024/06/05 18:33
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
有不少文章讨论怎么在两个页面间传递引用,如果你页面上的对象设置为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!
<
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击