页面之间传值的几种方法

 

1.Get方式

   发送页代码:Response.Redirect("WebFormA2.aspx?name=" + TextBox1.Text);

   接收页代码:

             this.TextBox1.Text = Request["name"];
            //this.TextBox1.Text=Request.Params["name"];
            //this.TextBox1.Text=Request.QueryString["name"];

2.使用内存变量

    发送页代码:

           Session["name"] = this.TextBox1.Text;
            //Application["name"]=this.TextBox1.Text;

     接收页代码:

            this.TextBox1.Text = (string)Session["name"];
          //this.TextBox1.Text = (string)Application["name"];

3.Post方式

     发送页代码:

       <form />   
        </form>

       接收页代码:

           if (Request.Form["txtname"] != null)
            {
                TextBox1.Text = Request.Form["txtname"];
            }

    注意:<form>中不能带runat="server".否则不起作用.

            <form>中的mothod="post"

4.静态变量

         发送页代码:

               //定义一个公共变量
                 public static string str = "";
                 protected void Button1_Click(object sender, EventArgs e)
                {
                     str = this.TextBox1.Text;
                     Response.Redirect("WebFormD2.aspx");
                 }

           接收页代码:  this.TextBox1.Text = WebFormD1.str;

 5.Context.Handler获取控件

        发送页代码:

              <asp:TextBox ID="TextBox1" Text="Andy" runat="server"></asp:TextBox>
               <asp:Button ID="Button1" runat="server" Text="进入WebFormE2.aspx" OnClick="Button1_Click" />

              //Button1_Click事件代码

              Response.Redirect("WebFormE2.aspx");

         接收页面代码:

               //获取post过来的页面对象
            if (Context.Handler is WebFormE1)
            {
                //取得页面对象
                WebFormE1 poster = (WebFormE1)Context.Handler;

                //取得控件               
                this.TextBox1.Text = ((TextBox)poster.FindControl("TextBox1")).Text;
                //this.TextBox1.Text = poster.TextBox1.Text;
            }

 

6.Context.Handler获取公共变量

        发送页代码:

             //定义一个公共变量
             public string strname = "Andy";
             protected void Button1_Click(object sender, EventArgs e)
            {            
                 Response.Redirect("WebFormE2.aspx");           

            }

       接收页代码:

            //获取post过来的页面对象
            if (Context.Handler is WebFormF1)
            {
                //取得页面对象
                WebFormF1 poster = (WebFormF1)Context.Handler;
                this.TextBox1.Text = poster.strname;
            }

 

7.Context.items变量

     发送页代码:

            protected void Button1_Click(object sender, EventArgs e)
            {
                  Context.Items["name"] = TextBox1.Text;
                  Server.Transfer("WebFormG2.aspx");
             }

       接收页代码:

              //获取post过来的页面对象
            if (Context.Handler is WebFormG1)
            {
                this.TextBox1.Text = Context.Items["name"].ToString();
            }

 

         本文抄自<亮剑.NET>,记一下笔记!

分类:

技术点:

相关文章: