1 通过HTTPCookies类进行创建
创建Cookies:
HttpCookie StudentCookies = new HttpCookie("StudentCookies"); StudentCookies.Value = TextBox1.Text; StudentCookies.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(StudentCookies);
读取Cookies:
string roll = Request.Cookies["StudentCookies"].Value;
2直接通过Response进行创建
创建Cookies:
Response.Cookies["StudentCookies"].Value = TextBox1.Text; Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);
读取Cookies:
string roll = Request.Cookies["StudentCookies"].Value;
3多值的存储
创建Cookies:
Response.Cookies["StudentCookies"]["RollNumber"] = TextBox1.Text; Response.Cookies["StudentCookies"]["FirstName"] = "Abhimanyu"; Response.Cookies["StudentCookies"]["MiddleName"] = "Kumar"; Response.Cookies["StudentCookies"]["LastName"] = "Vatsa"; Response.Cookies["StudentCookies"]["TotalMarks"] = "499"; Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);
读取Cookies:
string roll; roll = Request.Cookies["StudentCookies"]["RollNumber"]; roll = roll + " " + Request.Cookies["StudentCookies"]["FirstName"]; roll = roll + " " + Request.Cookies["StudentCookies"]["MiddleName"]; roll = roll + " " + Request.Cookies["StudentCookies"]["LastName"]; roll = roll + " " + Request.Cookies["StudentCookies"]["TotalMarks"]; Label1.Text = roll;
4.删除Cookies的方法
删除Cookies的实质是修改它的过期时间,代码如下:
if (Request.Cookies["StudentCookies"] != null) { Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(-1); Response.Redirect("Result.aspx"); //to refresh the page }