CopyCode The Code is 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 button#click (Object sender, eventargs E)
{
Httpcookie cookie = request. Cookies ["mycook"];
// Enable the service if it is not available
If (cookie = NULL)
{
Cookie = new httpcookie ("mycook"); // initialize and set the cookie name
Datetime dt = datetime. now;
Timespan Ts = new timespan (0, 0, 10, 0, 0 );
Cookie. expires = DT. Add (TS );
Response. appendcookie (cookie );
}
// There are five checkboxes on the interface, respectively saving the checkbox values to the cookie
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>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> No title page </title>
<SCRIPT type = "text/JavaScript">
Function clearcookie (){
VaR exp = new date ();
Exp. settime (exp. gettime ()-1 );
VaR A = getcookie ('mycook ');
Alert ();
VaR B = Name + "=" + A + "; expires =" + exp. tostring ();
Document. Cookie = B;
Alert (B );
}
Function getcookie (name)
// Obtain the original cookie value
{
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) {// obtain the cookie value whose name is offset.
VaR endstr = Document. Cookie. indexof (";", offset );
If (endstr =-1)
Endstr = Document. Cookie. length;
Return Unescape (document. Cookie. substring (offset, endstr ));
}
</SCRIPT>
</Head>
<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 = "button#click"/>
<A href = "cwz. aspx"> post </a>
<Input type = "button" value = "Clear cookie" onclick = "clearcookie ();"/>
</Div>
</Form>
</Body>
</Html>