如何JS写入cookie asp.net来读取这个cookie 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没用过,但是感觉应该js和.net利用session可以直接交互吧。关注,学习 http://blog.csdn.net/ligaofeng/archive/2007/05/07/1599646.aspx 之前写了个示例,给你看看,希望对你有所帮助,给分吧,呵呵<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookie.aspx.cs" Inherits="easy_cookie" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>无标题页</title> <script language="javascript"> //根据名称和参数读取cookie值 // // function GetCookie(name,params)// {// var originstr=document.cookie;// var tmp;// var reg=new RegExp("(?<=a=(\\w+?=\\w+?\&)*?ip=)\\w+(?=([&;])?)","gi"); // tmp=reg.exec(originstr);// return tem;// } function GetCookie(name) { m=document.cookie; re1=new RegExp("(?!\w)"+name+"=[^;]+",""); re2=new RegExp("^"+name+"=",""); try { var a=m.match(re1)[0]; } catch(e) { return null } eval("var o="+a.replace(re2,"{").replace(/&/g,"',").replace(/=/g,":\'")+"'}"); return o; } function ReadCookie() { var var1=document.form1.txtName.value; document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP; } function WriteCookie() { document.cookie=document.form1.txtName.value+"=IP="+document.form1.txtIP.value; } function ClearCookie() { document.cookie=""; } </script></head><body> <form id="form1" runat="server"> <table border="0" cellpadding="0" cellspacing="0" width="778px"> <tr> <td style="height: 100px; width: 778px;" align="center" valign="middle"> Cookie示例(对IP的操作)</td> </tr> <tr> <td align="center" style="width: 778px"> <br /> <asp:Label ID="Label1" runat="server" Text="名称"></asp:Label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /> <asp:Label ID="Label2" runat="server" Text="IP"></asp:Label> <asp:TextBox ID="txtIP" runat="server"></asp:TextBox><br /> <asp:Label ID="Label3" runat="server" Text="读取IP"></asp:Label> <asp:TextBox ID="txtReadIP" runat="server"></asp:TextBox><br /> </td> </tr> <tr> <td align="center" style="height: 25px; width: 778px;"> <br /> <br /> <asp:Button ID="btnRun" runat="server" Text="服务器设置Cookie" OnClick="Button1_Click" /> <asp:Button ID="btnGet" runat="server" Text="服务器读取Cookie" OnClick="Button2_Click" Width="158px" /> <asp:Button ID="Button3" runat="server" Text="服务器清除Cookie" OnClick="Button3_Click" /> <br /> <button type="button" onclick="WriteCookie()"> 客户端设置Cookie</button> <button type="button" onclick="ReadCookie()"> 客户端读取Cookie</button> <button type="button" onclick="ClearCookie()"> 客户端清除Cookie</button><br /> </td> </tr> </table> </form></body></html>cs代码:using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class easy_cookie : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } //写入 protected void Button1_Click(object sender, EventArgs e) { if (Request.Cookies[txtName.Text] == null) { HttpCookie cookie = new HttpCookie(txtName.Text); cookie["IP"] = txtIP.Text; Response.Cookies.Add(cookie); } else { Response.Cookies[txtName.Text]["IP"] = txtIP.Text; // Response.Write("<script>alert('已存在');</script>"); } } //读取 protected void Button2_Click(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies[txtName.Text]; if (cookie!=null) { txtReadIP.Text = cookie["IP"]; } } //清除cookie protected void Button3_Click(object sender, EventArgs e) { for (int i = 0; i < Request.Cookies.Count; i++) { HttpCookie cookie = Request.Cookies[i]; cookie.Expires = DateTime.Now.AddDays(-1); } //Response.Write(Request.Cookies.Count); // HttpCookie cookie = Request.Cookies; //cookie.Expires = DateTime.Now.Add(-1); }} 重新发一下.<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "cookie.aspx.cs " Inherits= "easy_cookie " %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns= "http://www.w3.org/1999/xhtml "> <head runat= "server "> <title> 无标题页 </title> <script language= "javascript "> //根据名称和参数读取cookie值 // // function GetCookie(name,params) // { // var originstr=document.cookie; // var tmp; // var reg=new RegExp( "(? <=a=(\\w+?=\\w+?\&)*?ip=)\\w+(?=([&;])?) ", "gi "); // tmp=reg.exec(originstr); // return tem; // } function GetCookie(name) { m=document.cookie; re1=new RegExp( "(?!\w) "+name+ "=[^;]+ ", " "); re2=new RegExp( "^ "+name+ "= ", " "); try { var a=m.match(re1)[0]; } catch(e) { return null } eval( "var o= "+a.replace(re2, "{ ").replace(/&/g, " ', ").replace(/=/g, ":\ ' ")+ " '} "); return o; } function ReadCookie() { var var1=document.form1.txtName.value; document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP; } function WriteCookie() { document.cookie=document.form1.txtName.value+ "=IP= "+document.form1.txtIP.value; } function ClearCookie() { document.cookie= " "; } </script> </head> <body> <form id= "form1 " runat= "server "> <table border= "0 " cellpadding= "0 " cellspacing= "0 " width= "778px "> <tr> <td style= "height: 100px; width: 778px; " align= "center " valign= "middle "> Cookie示例(对IP的操作) </td> </tr> <tr> <td align= "center " style= "width: 778px "> <br /> <asp:Label ID= "Label1 " runat= "server " Text= "名称 "> </asp:Label> <asp:TextBox ID= "txtName " runat= "server "> </asp:TextBox> <br /> <asp:Label ID= "Label2 " runat= "server " Text= "IP "> </asp:Label> <asp:TextBox ID= "txtIP " runat= "server "> </asp:TextBox> <br /> <asp:Label ID= "Label3 " runat= "server " Text= "读取IP "> </asp:Label> <asp:TextBox ID= "txtReadIP " runat= "server "> </asp:TextBox> <br /> </td> </tr> <tr> <td align= "center " style= "height: 25px; width: 778px; "> <br /> <br /> <asp:Button ID= "btnRun " runat= "server " Text= "服务器设置Cookie " OnClick= "Button1_Click " /> <asp:Button ID= "btnGet " runat= "server " Text= "服务器读取Cookie " OnClick= "Button2_Click " Width= "158px " /> <asp:Button ID= "Button3 " runat= "server " Text= "服务器清除Cookie " OnClick= "Button3_Click " /> <br /> <button type= "button " onclick= "WriteCookie() "> 客户端设置Cookie </button> <button type= "button " onclick= "ReadCookie() "> 客户端读取Cookie </button> <button type= "button " onclick= "ClearCookie() "> 客户端清除Cookie </button> <br /> </td> </tr> </table> </form> </body> </html>cs代码: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class easy_cookie : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //写入 protected void Button1_Click(object sender, EventArgs e) { if (Request.Cookies[txtName.Text] == null) { HttpCookie cookie = new HttpCookie(txtName.Text); cookie[ "IP "] = txtIP.Text; Response.Cookies.Add(cookie); } else { Response.Cookies[txtName.Text][ "IP "] = txtIP.Text; // Response.Write( " <script> alert( '已存在 '); </script> "); } } //读取 protected void Button2_Click(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies[txtName.Text]; if (cookie!=null) { txtReadIP.Text = cookie[ "IP "]; } } //清除cookie protected void Button3_Click(object sender, EventArgs e) { for (int i = 0; i < Request.Cookies.Count; i++) { HttpCookie cookie = Request.Cookies[i]; cookie.Expires = DateTime.Now.AddDays(-1); } //Response.Write(Request.Cookies.Count); // HttpCookie cookie = Request.Cookies; //cookie.Expires = DateTime.Now.Add(-1); } } bnb2626是可以的,最好再加上一些编码~~ 不好意思,这几天忙着每看CSDN,今天看了,给分了谢谢 非哥 static 的确可以保留,这样好吗? c# asp问题 webchart 的问题 IIS 500错误问题 用javascript怎样在一个页面中访问另一个页面控件的属性 理论题!!!!!!!求助!!!! 当我对Web程序启动编译时,出现了如下错误,请各位高手现身出来指点。 菜鸟问题 a.aspx 文件中用vb.net,然后在a.aspx.vb中也用vb.net 行吗? 一个简单的问题向大家求助(高分悬赏) 怎样提取TextBox里面的数字? 关于如何动态构建一个计算公式的表达式????
关注,学习
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title> <script language="javascript">
//根据名称和参数读取cookie值
//
// function GetCookie(name,params)
// {
// var originstr=document.cookie;
// var tmp;
// var reg=new RegExp("(?<=a=(\\w+?=\\w+?\&)*?ip=)\\w+(?=([&;])?)","gi");
// tmp=reg.exec(originstr);
// return tem;
// } function GetCookie(name)
{
m=document.cookie;
re1=new RegExp("(?!\w)"+name+"=[^;]+","");
re2=new RegExp("^"+name+"=","");
try
{
var a=m.match(re1)[0];
}
catch(e)
{
return null
}
eval("var o="+a.replace(re2,"{").replace(/&/g,"',").replace(/=/g,":\'")+"'}");
return o;
} function ReadCookie()
{
var var1=document.form1.txtName.value;
document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP;
}
function WriteCookie()
{
document.cookie=document.form1.txtName.value+"=IP="+document.form1.txtIP.value;
}
function ClearCookie()
{
document.cookie="";
}
</script></head>
<body>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0" width="778px">
<tr>
<td style="height: 100px; width: 778px;" align="center" valign="middle">
Cookie示例(对IP的操作)</td>
</tr>
<tr>
<td align="center" style="width: 778px">
<br />
<asp:Label ID="Label1" runat="server" Text="名称"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="IP"></asp:Label>
<asp:TextBox ID="txtIP" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="读取IP"></asp:Label>
<asp:TextBox ID="txtReadIP" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td align="center" style="height: 25px; width: 778px;">
<br />
<br />
<asp:Button ID="btnRun" runat="server" Text="服务器设置Cookie" OnClick="Button1_Click" />
<asp:Button ID="btnGet" runat="server" Text="服务器读取Cookie" OnClick="Button2_Click"
Width="158px" /> <asp:Button ID="Button3" runat="server" Text="服务器清除Cookie"
OnClick="Button3_Click" />
<br />
<button type="button" onclick="WriteCookie()">
客户端设置Cookie</button>
<button type="button" onclick="ReadCookie()">
客户端读取Cookie</button>
<button type="button" onclick="ClearCookie()">
客户端清除Cookie</button><br />
</td>
</tr>
</table>
</form>
</body>
</html>cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class easy_cookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
//写入
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies[txtName.Text] == null)
{
HttpCookie cookie = new HttpCookie(txtName.Text);
cookie["IP"] = txtIP.Text;
Response.Cookies.Add(cookie);
}
else
{
Response.Cookies[txtName.Text]["IP"] = txtIP.Text;
// Response.Write("<script>alert('已存在');</script>");
}
}
//读取
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies[txtName.Text];
if (cookie!=null)
{
txtReadIP.Text = cookie["IP"];
}
}
//清除cookie
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Cookies.Count; i++)
{
HttpCookie cookie = Request.Cookies[i];
cookie.Expires = DateTime.Now.AddDays(-1);
} //Response.Write(Request.Cookies.Count);
// HttpCookie cookie = Request.Cookies;
//cookie.Expires = DateTime.Now.Add(-1);
}
}
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> 无标题页 </title> <script language= "javascript ">
//根据名称和参数读取cookie值
//
// function GetCookie(name,params)
// {
// var originstr=document.cookie;
// var tmp;
// var reg=new RegExp( "(? <=a=(\\w+?=\\w+?\&)*?ip=)\\w+(?=([&;])?) ", "gi ");
// tmp=reg.exec(originstr);
// return tem;
// } function GetCookie(name)
{
m=document.cookie;
re1=new RegExp( "(?!\w) "+name+ "=[^;]+ ", " ");
re2=new RegExp( "^ "+name+ "= ", " ");
try
{
var a=m.match(re1)[0];
}
catch(e)
{
return null
}
eval( "var o= "+a.replace(re2, "{ ").replace(/&/g, " ', ").replace(/=/g, ":\ ' ")+ " '} ");
return o;
} function ReadCookie()
{
var var1=document.form1.txtName.value;
document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP;
}
function WriteCookie()
{
document.cookie=document.form1.txtName.value+ "=IP= "+document.form1.txtIP.value;
}
function ClearCookie()
{
document.cookie= " ";
}
</script> </head>
<body>
<form id= "form1 " runat= "server ">
<table border= "0 " cellpadding= "0 " cellspacing= "0 " width= "778px ">
<tr>
<td style= "height: 100px; width: 778px; " align= "center " valign= "middle ">
Cookie示例(对IP的操作) </td>
</tr>
<tr>
<td align= "center " style= "width: 778px ">
<br />
<asp:Label ID= "Label1 " runat= "server " Text= "名称 "> </asp:Label>
<asp:TextBox ID= "txtName " runat= "server "> </asp:TextBox> <br />
<asp:Label ID= "Label2 " runat= "server " Text= "IP "> </asp:Label>
<asp:TextBox ID= "txtIP " runat= "server "> </asp:TextBox> <br />
<asp:Label ID= "Label3 " runat= "server " Text= "读取IP "> </asp:Label>
<asp:TextBox ID= "txtReadIP " runat= "server "> </asp:TextBox> <br />
</td>
</tr>
<tr>
<td align= "center " style= "height: 25px; width: 778px; ">
<br />
<br />
<asp:Button ID= "btnRun " runat= "server " Text= "服务器设置Cookie " OnClick= "Button1_Click " />
<asp:Button ID= "btnGet " runat= "server " Text= "服务器读取Cookie " OnClick= "Button2_Click "
Width= "158px " /> <asp:Button ID= "Button3 " runat= "server " Text= "服务器清除Cookie "
OnClick= "Button3_Click " />
<br />
<button type= "button " onclick= "WriteCookie() ">
客户端设置Cookie </button>
<button type= "button " onclick= "ReadCookie() ">
客户端读取Cookie </button>
<button type= "button " onclick= "ClearCookie() ">
客户端清除Cookie </button> <br />
</td>
</tr>
</table>
</form>
</body>
</html>
cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; public partial class easy_cookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
//写入
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies[txtName.Text] == null)
{
HttpCookie cookie = new HttpCookie(txtName.Text);
cookie[ "IP "] = txtIP.Text;
Response.Cookies.Add(cookie);
}
else
{
Response.Cookies[txtName.Text][ "IP "] = txtIP.Text;
// Response.Write( " <script> alert( '已存在 '); </script> ");
}
}
//读取
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies[txtName.Text];
if (cookie!=null)
{
txtReadIP.Text = cookie[ "IP "];
}
}
//清除cookie
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Cookies.Count; i++)
{
HttpCookie cookie = Request.Cookies[i];
cookie.Expires = DateTime.Now.AddDays(-1);
} //Response.Write(Request.Cookies.Count);
// HttpCookie cookie = Request.Cookies;
//cookie.Expires = DateTime.Now.Add(-1);
}
}
谢谢