ASP.NET中使用Server.Transfer()方法在页间传值 
ASP.NET Server.Transfer()是在两个页面之间进行传值的好方法,从A页面Transfer到B页面时,就可以在B页面通过Context.Handler获得A页面的一个类的实例,从而在B调用A的各个成员对象。下面的示例建立了WebForm1和WebForm2,通过Server.Transfer()方法演示在WebForm2中读取WebForm1的文本框、读取属性、通过Context传值、调用WebForm1的方法等:WebForm1上放置一个TextBox1和一个Button1,程序如下: public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   Context.Items.Add("Context","Context from Form1");
  }
  public string Time
  {
   get{return DateTime.Now.ToString();}
  }
  public string TestFun()
  {
   return "Function of WebForm1 Called";
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   InitializeComponent();
   base.OnInit(e);
  }
  
  private void InitializeComponent()
  {    
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);  }
  #endregion  private void Button1_Click(object sender, System.EventArgs e)
  {
   Server.Transfer("WebForm2.aspx", true);
  }
在WebForm2上放置一个Literal1控件,程序如下: public class WebForm2 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Literal Literal1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   string strTxt="";
   WebForm1 oForm=(WebForm1)this.Context.Handler;
   strTxt+="Value of Textbox:"+Request.Form["TextBox1"] +"<br>";
   strTxt+="Time Property:"+oForm.Time +"<br>";
   strTxt+="Context String:"+Context.Items["Context"].ToString() +"<br>";
   strTxt+=oForm.TestFun() +"<br>";
   Literal1.Text =strTxt;
  }  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   InitializeComponent();
   base.OnInit(e);
  }
  
  private void InitializeComponent()
  {    
   this.Load += new System.EventHandler(this.Page_Load);  }
  #endregion
 }补充说明,就是Transfer方法的第二个参数指示是否保留页面的Form和QuerryString的值,你可以试着把它设为False,则在WebForm2中将读不到TextBox1的值。
我按上面说的在VS。NET里设置老是不行
WebForm2到  WebForm1 oForm=(WebForm1)this.Context.Handler; 提示转换无效我是错在那里?我把两个WebForm放到Frameset里还要改动吗?

解决方案 »

  1.   

    WebForm1 oForm=(WebForm1)this;呢
      

  2.   

    你是点击Button1切换到webform2的吗?webform1的类名是webform1吗?
      

  3.   

    具体代码如下(请仔细对比页面名称的变化):
    要传递值的页面asm_content_view.aspx
    /// <summary>
    /// 搜索值
    /// </summary>
    public string strText
    {
      get
      {
        return(this.txtsearch.Text);
      }
    }/// <summary>
    /// 搜索类型
    /// </summary>
    public string strType
    {
      get
      {
        return(this.listsearch.SelectedValue);
      }
    }/// <summary>
    /// 搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void butsearch_Click(object sender, System.EventArgs e)
    {
      this.Server.Transfer("asm_content_list.aspx");
    }
    要接收的页面
    //查看有无从asm_content_view页面转来的搜索对象
    string strname = Context.Handler.ToString().ToLower();
    if( strname == "asp.asm_content_view_aspx")
    {
      mydata.content.page.asm_content_view myform = new asm_content_view();        
      myform = (mydata.content.page.asm_content_view)Context.Handler;
      this.txtsearch.Text = myform.strText;
      this.listsearch.SelectedValue = myform.strType;
    }
      

  4.   

    我用了Frameset,左边是WebForm2,右边是WebForm1。点了WebForm1 里的button,把WebForm1 里的textbox.txt的值取后放到WebForm2里的label1.txt里去。我看了下觉得没错啊。怎么就不行了?那位大哥有这方面的实例下吗?我下一个看哈。
    或帮我看看我那里有错。
      

  5.   

    WebForm2的这行  WebForm1 oForm=(WebForm1)this.Context.Handler; 提示转换无效
      

  6.   

    WebForm1 oForm=(WebForm1)Context.Handler
    试试看
      

  7.   

    WebForm1 oForm=(WebForm1)Context.Handler
    不行都一样提示无效转换
      

  8.   

    异常详细信息: System.InvalidCastException: 指定的转换无效。
      

  9.   

    Server.Transfer()之后可以使用label1.text=Page.Request.Form["textbox"]
    或者用Server.Execute()试试 使用另一页执行当前请求
      

  10.   

    为什么不做成用户控件再调用
    http://community.csdn.net/Expert/topic/4125/4125275.xml?temp=.7877619
      

  11.   

    haonanernet(与时俱进) 
    不会用,我是新手。
    大哥多给点资料吧。
      

  12.   

    haonanernet(与时俱进) 
    那贴的内容很符合我要做到的。不知道怎么做。~:)
      

  13.   

    <cc:Header id="head" runat='server' />    //这个不懂?id="head"Label l = head.FindControl("label1") as Label;
    l.Text = "abc";
    用户控件写好了后怎么掉用?