Asp.net頁面之間傳遞參數的幾種方法

來源:互聯網
上載者:User

因為在項目中需要在兩個頁面之間傳遞一些參數,所以總結出以下幾個傳遞參數的方法:
第一種方法:
通過URL連結地址傳遞
 send.aspx:
  protected void Button1_Click(object sender, EventArgs e)
    {
        Request.Redirect("Default2.aspx?username=honge");
    }
 receive.aspx:
 string username = Request.QueryString["username"];這樣可以得到參數值。

第二種方法:
send.aspx
<form id="form1" runat="server" action="receive.aspx" method=post>
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="username" runat="server"></asp:TextBox>
</div>
    </form>
receive.aspx
string username = Ruquest.Form["receive"];

第三種方法:
send.aspx:
  protected void Button1_Click(object sender, EventArgs e)
    {
        Session["username"] = "honge";
        Request.Redirect("Default2.aspx");
    }
 receive.aspx:
 string username = Session["username"];這樣可以得到參數值。

第四種方法:
send.aspx:
  protected void Button1_Click(object sender, EventArgs e)
    {
        Application["username"] = "honge";
        Request.Redirect("Default2.aspx");
    }
 receive.aspx:
 string username = Application["username"];這樣可以得到參數值。

第五種方法:
send.aspx:
  public string Name
    {
        get {
            return "honge";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("Default2.aspx");
    }
 receive.aspx:
   send d = Context.Handler as send ;
        if (d != null)
        {
            Response.Write(d.Name);這樣可以得到參數值。
        }

如果在asp.net 2.0中還可以這樣用:

PreviousPage d = Context.Handler as PreviousPage ;
 if (d != null)
        {
            Response.Write(d.Name);這樣可以得到參數值。
        }
也可以這樣用:
send.aspx:
<asp:Button ID="btnSubmit" runat="server" PostBackUrl="~/reveive.aspx" Text="Submit" />
receive.aspx:
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
string name = PreviousPage.Name;這樣可以得到參數值。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.