.NET 頁面間傳值的幾種方法

來源:互聯網
上載者:User

標籤:http   io   os   使用   sp   for   on   資料   bs   

1. QueryString

  這是最簡單的傳值方式,但缺點是傳的值會顯示在瀏覽器的地址欄中且不能傳遞對象,只適用於傳遞簡單的且安全性要求不高的數值。

  傳遞:  location.href="WebForm2.aspx?name=" + yourName&&name2="+ yourName2;

  接收:  string name = Request.QueryString["name"];

 

2. Form

  傳遞:  根據表單內控制項的name和value傳遞,如:

       <form id="form1" method="post">

          <input type="text" name="name" value="xxx" />

       </form>

       表單需要被提交<input type="submit" /> 或者 document.getElementById("form1").submit();

  接收:  string name = Request.Form["name"];

 

3. Session

  需要注意的是在Session變數儲存過多的資料會消耗比較多的伺服器資源,在使用session時應該使用一些清理動作來去除一些不需要的session。

  移除:  Session.Remove("name");

  傳遞:  Session["name"] = yourName;

  接收:  string name=Session["name"].ToString();

 

4. Cookie

  傳遞:  HttpCookie cookie_name = new HttpCookie("name");

       cookie_name.Value = yourName;

       Response.AppendCookie(cookie_name);

  接收:  Request.Cookies["name"].Value;

 

5. Application

  Application對象的作用範圍是整個全域,也就是說對所有使用者都有效。其常用的方法用Lock和UnLock。

  傳遞: Application["name"] = name;

  接收: Application.Lock();

      string name = Application["name"].ToString(); 

      Application.UnLock();

      //Lock的目的是為了防止被多線程篡改,保證這一時刻只有自己在改

 

6. Server.Transfer

  傳遞:

  WebForm1寫好需要被傳值的屬性  如:

  public string Name { get{ return txtName.Text; } }

  執行Server.Transfer("WebForm2.aspx");

   

  接收:

  WebForm2中接收參數:

  WebForm1 wf1=(WebForm1)Context.Handler;

  Response.Write( wf1.Name );

.NET 頁面間傳值的幾種方法

聯繫我們

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