与session、applocation不同的是cookie是将数据存放于客户端的磁盘上,但cookie中只能含有少量的信息,通常不超过4096个字节。
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie readcookie = Request.Cookies["user"];
if (readcookie != null)
{
Textbox1.Text = readcookie.Value;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("user");
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 6, 0);
cookie.Expires = dt.Add(ts);
cookie.Value = Textbox1.Text;
Response.AppendCookie(cookie);
}