asp.net頁面與頁面之間傳參數值

來源:互聯網
上載者:User

一、利用POST傳值

傳值asp檔案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>

接受asp檔案receive.aspx

 代碼如下 複製代碼

string username = Ruquest.Form["receive"];

一、get方法傳值

QueryString 也叫查詢字串, 這種方法將要傳遞的資料附加在網頁地址(URL)後面進行傳遞。如頁面A.aspx 跳轉到頁面B.aspx,可以用Request.Redirect("B.aspx?參數名稱=參數值")方法,也可以用超連結:,頁面跳轉後,在目標頁 面中可用Ruquest["參數名稱"]來接收參數。使用QuerySting 方法的優點是實現簡單, 不使用伺服器資源;缺點是傳遞的值會顯示在瀏覽器的地址欄上,有被篡改的風險,不能傳遞對象,只有在通過URL 請求頁時查詢字串才是可行的

下面的代碼片斷示範了如何?這個方法:

源頁面代碼:

 代碼如下 複製代碼

private void Button1_Click (object sender, System.EventArgs e)
{
   string url;
   url="anotherwebform.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
   Response.Redirect(url);
}

目標頁面代碼:

 代碼如下 複製代碼

private void Page_Load(object sender, System.EventArgs e)
{
   Label1.Text=Request.QueryString["name"];
   Label2.Text=Request.QueryString["email"];
}

相關文章

聯繫我們

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