Server.Transfer和Context.Items

 ////page  :A .aspx
public partial class A : System.Web.UI.Page  
   {  
       public string Name  
       {  
           get { return Button1.Text; }  
       }  
       protected void Page_Load(object sender, EventArgs e)  
       {  
       }  
       protected void Button1_Click(object sender, EventArgs e)  
       {  
           Server.Transfer("B.aspx");  
       }  
       protected void Button2_Click(object sender, EventArgs e)  
       {  
           ArrayList list = new ArrayList(3);  
           list.Add("This list ");  
           list.Add("is for ");  
           list.Add("FormerPage to see.");  
           Context.Items["FormerPageList1"] = list;  
           Server.Transfer("B.aspx");  
       }  
       internal string GetDataSet()  
       {  
           return "A.aspx中的方法";  
       }  
   } 
 public partial class A : System.Web.UI.Page
    {
        public string Name
        {
            get { return Button1.Text; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Server.Transfer("B.aspx");
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList(3);
            list.Add("This list ");
            list.Add("is for ");
            list.Add("FormerPage to see.");
            Context.Items["FormerPageList1"] = list;
            Server.Transfer("B.aspx");
        }
        internal string GetDataSet()
        {
            return "A.aspx中的方法";
        }
    }

////page  :B .aspx

public partial class B : System.Web.UI.Page  
   {  
       protected void Page_Load(object sender, EventArgs e)  
       {  
           if (!IsPostBack)  
           {  
               A apage = (A)Context.Handler;  
               Response.Write("获取A.aspx上按钮的Text值:" + apage.Name+"<br/>");  
 
               try 
               {  
                   A former = (A)Context.Handler;  
                   txtFromParentPage.Text = "获取A中定义的ClassName公共属性:" + former.Name; //获取A中定义的ClassName公共属性  
                   //获取在A中的上下文字典中添加的ArrayList: Context.Items["FormerPageList1"]  
                   //获取Contex字典项并强制转换类型:  
                   ArrayList list = Context.Items["FormerPageList1"] as ArrayList;  
                   string ds = former.GetDataSet(); //调用A中定义的GetDataSet()公共方法  
                   Response.Write(ds+"<br/>");  
                   Response.Write("A.aspx中的ArrayList" + "<br/>");  
                   for (int i = 0; i < list.Count; i++)  
                   {  
                       Response.Write(list[i] + "<br/>");  
                   }  
               }  
               catch 
               {  
                   Response.Write("Error get data from parent transfer page!");  
               }  
           }  
       }  
   } 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Rookie_CEO/archive/2010/01/06/5143079.aspx

 

posted on 2010-06-07 21:28  henzy  阅读(234)  评论(0编辑  收藏  举报

导航