在网上看到的一个Server.Transfer例子,但我用Default2 myDF=(Default2)this.Context.Handler时根本就不行,不能实例其他页面的类的吗?那例子为什么又可以的?WebForm1上放置一个TextBox1和一个Button1,程序如下:1.        public class WebForm1 : System.Web.UI.Page   2.        {   3.            protected System.Web.UI.WebControls.TextBox TextBox1;   4.            protected System.Web.UI.WebControls.Button Button1;   5.            private void Page_Load(object sender, System.EventArgs e)   6.            {   7.                Context.Items.Add("Context", "Context from Form1");   8.            }   9.            /// <summary>   10.         /// 页面的数据作为public属性被后面的页面程序访问   11.         /// </summary>   12.         public string Time   13.         {   14.             get { return DateTime.Now.ToString(); }   15.         }   16.         public string TestFun()   17.         {   18.             return "Function of WebForm1 Called";   19.         }  20.         #region Web 窗体设计器生成的代码   21.         override protected void OnInit(EventArgs e)   22.         {   23.             InitializeComponent();   24.             base.OnInit(e);   25.         }   26.         private void InitializeComponent()   27.         {   28.             this.Button1.Click += new System.EventHandler(this.Button1_Click);   29.             this.Load += new System.EventHandler(this.Page_Load);   30.         }  31.         #endregion   32.         private void Button1_Click(object sender, System.EventArgs e)   33.         {   34.             Server.Transfer("WebForm2.aspx", true);   35.         }   36.     }  在WebForm2上放置一个Literal1控件,程序如下:1.        public class WebForm2 : System.Web.UI.Page   2.        {   3.            protected System.Web.UI.WebControls.Literal Literal1;   4.            private void Page_Load(object sender, System.EventArgs e)   5.            {   6.                string strTxt = "";   7.                if (!IsPostBack)   8.                {   9.                    WebForm1 oForm = (WebForm1)this.Context.Handler;   10.                 strTxt += "Value of Textbox:" + Request.Form["TextBox1"] + "<br>";   11.                 strTxt += "Time Property:" + oForm.Time + "<br>";   12.                 strTxt += "Context String:" + Context.Items["Context"].ToString() + "<br>";   13.                 strTxt += oForm.TestFun() + "<br>";   14.                 Literal1.Text = strTxt;   15.             }   16.         }  17.         #region Web 窗体设计器生成的代码   18.         override protected void OnInit(EventArgs e)   19.         {   20.             InitializeComponent();   21.             base.OnInit(e);   22.         }   23.         private void InitializeComponent()   24.         {   25.             this.Load += new System.EventHandler(this.Page_Load);   26.         }  27.         #endregion   28.     }  

解决方案 »

  1.   

    VS05里可能不行,VS03应该没问题
      

  2.   


    Server.Transfer传值小例子 在查询页面中设置如下公有属性(QueryPage.aspx):
    public class QueryPage : System.Web.UI.Page
    {
       protected System.Web.UI.WebControls.TextBox txtStaDate;
       protected System.Web.UI.WebControls.TextBox txtEndDate;
    ...
       /// <summary>
       /// 开始时间
       /// </summary>
       public string StaDate
       {
          get{ return this.txtStaDate.Text;}
          set{this.txtStaDate.Text = value;}
       }
    /// <summary>
    /// 结束时间
    /// </summary>
    public string EndDate
    {
       get{ return this.txtEndDate.Text;}
       set{this.txtEndDate.Text = value;}
    }
    ....
    private void btnEnter_Click(object sender, System.EventArgs e)
    {
       Server.Transfer("ResultPage.aspx");
    }
    }在显示查询结果页面(ResultPage.aspx):
    public class ResultPage : System.Web.UI.Page
    {
       private void Page_Load(object sender, System.EventArgs e)
       {      //转换一下即可获得前一页面中输入的数据
          QueryPage queryPage = ( QueryPage )Context.Handler;      Response.Write( "StaDate:" );
          Response.Write( queryPage.StaDate );
          Response.Write( "<br/>EndDate:" );
          Response.Write( queryPage.EndDate );
       }
    }
      

  3.   

    在2005下测试了一下没任何问题
    a页面代码:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="a.aspx.cs" Inherits="WebApplication.a" %><!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:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
        </form>
    </body>
    </html>namespace WebApplication
    {
        public partial class a : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }        public string testName
            {
                get { return DateTime.Now.ToString(); }
            }        protected void Button1_Click(object sender, EventArgs e)
            {
                Server.Transfer("b.aspx",true);
            }
        }
    }
    b页面代码:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="b.aspx.cs" Inherits="WebApplication.b" %><!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>namespace WebApplication
    {
        public partial class b : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    a p = (a)this.Context.Handler;
                    Response.Write(p.testName);
                }
            }
        }
    }