cookie的使用,一般分为传值与接收值,因此,下面将使用到两个页面分别实现这两种功能。
一、传值页面的代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void chuanzhi_Click(object sender, EventArgs e)
{
HttpCookie ck_animal = new HttpCookie("a");
ck_animal.Value = "大众计算机学习网";
ck_animal.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(ck_animal);
}
protected void qingchu_Click(object sender, EventArgs e)
{
Response.Cookies["a"].Expires = DateTime.MinValue;
}
}
二、接收值的页面代码
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["a"] != null)
{
string _mystr = Request.Cookies["a"].Value;
Response.Write(_mystr);
}
}
}