当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net通过js实现Cookie创建以及清除Cookie数组的代码

asp.net通过js实现Cookie创建以及清除Cookie数组的代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

tvb直播软件,引蛇出洞服务,最新毛衣编织款式

复制代码 代码如下:

<%@ 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"];
// 没有就初使化
if (cookie == null)
{
cookie = new httpcookie("mycook"); //初使化并设置cookie的名称
datetime dt = datetime.now;
timespan ts = new timespan(0, 0, 10, 0, 0);
cookie.expires = dt.add(ts);
response.appendcookie(cookie);
}
// 界面上有 5 个 checkbox,分别将 checkbox 的选值保存到 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;
}
// 遍历
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>无标题页</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)
//获得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) { //取得项名称为offset的cookie值
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="button1_click" />
<a href="cwz.aspx" >post</a>
<input type="button" value="清楚cookie" onclick="clearcookie();" />
</div>
</form>
</body>
</html>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网