Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "BLTZ.aspx.cs" inherits= "Bltz"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "Server" >
protected void Button1_Click (object sender, EventArgs e)
{
HttpCookie cookie = request.cookies["Mycook"];
Not on the first stage of the
if (cookie = null)
{
cookie = new HttpCookie ("Mycook"); First make and set the name of the cookie
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan (0, 0, 10, 0, 0);
Cookie. Expires = dt. ADD (TS);
Response.appendcookie (cookie);
}
There are 5 checkbox on the interface, saving the checkbox's selection to the cookie, respectively
for (int i = 1; i < 6; i++)
{
CheckBox control = this. FindControl (String.Format ("checkbox{0}", i)) as CheckBox;
if (control = = null) continue;
String key = String.Format ("num{0}", i);
String value = control. Checked.tostring ();
Cookie. Values[key] = value;
}
Traverse
foreach (string key in Cookie.) Values.allkeys)
{
String value = Cookie. Values[key];
Response.Write (String.Format ("subkey:{0}"; Subvalue:{1}<br/> ", key, value));
}
}
</script>
<title> Untitled Page </title>
<script type= "Text/javascript" >
function ClearCookie () {
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var a = GetCookie (' Mycook ');
alert (a);
var b = name + "=" + A + "; Expires= "+ exp.tostring ();
Document.cookie = b;
alert (b);
}
function GetCookie (name)
Get the original value of the cookie
{
var arg = name + "=";
var alen = Arg.length;
var clen = document.cookie.length;
alert (Document.cookie);
var i = 0;
while (I < Clen) {
var j = i + Alen;
if (Document.cookie.substring (i, j) = arg)
Return Getcookieval (j);
i = Document.cookie.indexOf ("", I) + 1;
if (i = = 0) break;
}
return null;
}
function Getcookieval (offset) {//Get cookie value of item name called offset
var endstr = document.cookie.indexOf (";", offset);
if (endstr = = 1)
Endstr = Document.cookie.length;
Return unescape (document.cookie.substring (offset, endstr));
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:checkbox id= "CheckBox1" runat= "Server"/>
<asp:checkbox id= "CheckBox2" runat= "Server"/>
<asp:checkbox id= "CheckBox3" runat= "Server"/>
<asp:checkbox id= "CheckBox4" runat= "Server"/>
<asp:checkbox id= "CHECKBOX5" runat= "Server"/>
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>
<a href= "Cwz.aspx" >Post</a>
<input type= "button" value= "Clear Cookie" onclick= "ClearCookie ();"/>
</div>
</form>
</body>