Asp.net uses js to create and clear Cookie arrays.

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.