Copy codeThe 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>