ASP.NET頁面之間傳遞值(3):Cookie

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   art   

這個也是大家常使用的方法,Cookie用於在使用者瀏覽器上儲存小塊的資訊,儲存使用者的相關資訊,比如使用者訪問某網站時使用者的ID,使用者的偏好等, 使用者下次訪問就可以通過檢索獲得以前的資訊。所以Cookie也可以在頁面間傳遞值。Cookie通過HTTP頭在瀏覽器和伺服器之間來回傳遞的。 Cookie只能包含字串的值,如果想在Cookie儲存整數值,那麼需要先轉換為字串的形式。

  與Session一樣,其是什對每一個使用者而言的,但是有個本質的區別,即Cookie是存放在用戶端的,而session是存放在伺服器端的。而且Cookie的使用要配合ASP.NET內建對象Request來使用。

  優點:1.使用簡單,是保持使用者狀態的一種非常常用的方法。比如在購物網站中使用者跨多個頁面表單時可以用它來保持使用者狀態。

  缺點:1.常常被人認為用來收集使用者隱私而遭到批評。

     2.安全性不高,容易偽造。

  

  使用方法:1.在源頁面的代碼中建立你需要傳遞的名稱和值構造Cookie對象:

1 HttpCookie objCookie = new HttpCookie("xiaobeicookie","Hello,xiaobeicookie!");2 Response.Cookies.Add(cookie); 

2.在目的頁面的代碼使用Cookie對象取出傳遞的值:Result = Request.Cookies[ "xiaobeicookie" ].Value;

 1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.WebControls; 6  7 public partial class JajaWeixinQianduanWeb_WoYaoDingCan_indextest : System.Web.UI.Page 8 { 9     protected void Page_Load(object sender, EventArgs e)10     {11 12     }13 14     protected void btn_chuanzhi_Click(object sender, EventArgs e)15     {16   HttpCookie objCookie = new HttpCookie("xiaobeicookie","Hello,xiaobeicookie!");17   Response.Cookies.Add(objCookie); 18     }19 }

indextestlist.aspx後台頁面:

 1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.WebControls; 6  7 public partial class JajaWeixinQianduanWeb_WoYaoDingCan_indextestlist : System.Web.UI.Page 8 { 9     protected void Page_Load(object sender, EventArgs e)10     {11      string myName1Value;12      myName1Value = Request.Cookies[ "xiaobeicookie" ].Value;13      }14 }

聯繫我們

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