asp.net through JS to create cookies and the elimination of Cookies array code _ Practical Tips

Source: Internet
Author: User
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>

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.