WebForm aspx頁面傳值---7種方式

來源:互聯網
上載者:User

標籤:ica   type   stp   tar   control   ted   find   ram   session   

1、get方式

發送頁

<form id="form1" runat="server">
    <div>
        <a href="WebForm2.aspx?name=5">調轉到Form2</a>
        <asp:Button ID="button2" Text="跳轉頁面" runat="server" onclick="button2_Click"/>
    </div>
</form>

protected void button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm2.aspx?name=5");

        }

接受頁

     this.Label1.Text = Request["name"];
            //this.Label2.Text = Request.Params["name"];
            //this.Label3.Text = Request.QueryString[0];

 

2、post方式

a\不帶 runat="server"形式

發送頁

<form id="form2" action="WebForm2.aspx" method="post">
        <input name="txtname" type="text" value="lilili"  />
        <input type="submit" value="提交網頁" />
</form>

接受頁

  this.Label1.Text =Request.Form["txtname"];

b\帶 runat="server"

發送頁

<form runat="server" id="form3">
        <input id="btnTransfer" type="button" onclick="post();" runat="server" value="跳轉" />
    </form>
    <form id="form4" method="post">
        <input type="text" runat="server" id="txtname" value="lili" />
    </form>
    <script type="text/javascript">
        function post() {
            form4.action = "WebForm2.aspx";
            form4.submit();
        }
    </script>

接受頁

  this.Label1.Text =Request.Form["txtname"];

 

3、Session 和 Application

            Session["name2"] = "sessontest";
            Application["name3"] = "applicationtest";

 

            this.Label2.Text =(string)Session["name2"];
            this.Label3.Text =(string)Application["name3"];

 

4、靜態變數

發送頁

 

        public static string statest="static string";

        protected void button2_Click(object sender, EventArgs e)
        {
            Server.Transfer("WebForm2.aspx");
        }

接受頁

  this.Label1.Text = WebForm1.statest;

 

5、Context.Handler 擷取控制項

發送頁

          <asp:TextBox ID="TextBox1" runat="server" Text="lilili"></asp:TextBox>
        <asp:Button ID="button2" Text="跳轉頁面" runat="server" onclick="button2_Click"/>

          protected void button2_Click(object sender, EventArgs e)
        {
            Server.Transfer("WebForm2.aspx");
        }

  

接受頁

         //擷取post傳過來的對象
            if (Context.Handler is WebForm1)
            {
                WebForm1 poster = (WebForm1)Context.Handler;
                this.Label1.Text = ((TextBox)poster.FindControl("TextBox1")).Text;
            }

 

6、Context.Handler 擷取公開變數

發送頁

     public string testpost = "testpost";
        protected void button2_Click(object sender, EventArgs e)
        {
            Server.Transfer("WebForm2.aspx");
        }

接受頁

        //擷取post傳過來的對象
            if (Context.Handler is WebForm1)
            {
                WebForm1 poster = (WebForm1)Context.Handler;
                this.Label2.Text = poster.testpost;
            }

 

7、Context.Items 變數

發送頁

        protected void button2_Click(object sender, EventArgs e)
        {
            Context.Items["name"] = "contextItems";
            Server.Transfer("WebForm2.aspx");
        }

接受頁

       //擷取post傳過來的對象
            if (Context.Handler is WebForm1)
            {
                this.Label3.Text = Context.Items["name"].ToString();
            }

WebForm aspx頁面傳值---7種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.