问题描述
我写的COOKIE,不关闭窗口没问题,关了窗口再打开,就找不到了,需要重新写,怎么回事啊?if(Request.Cookies["test"]==null){HttpCookiehc=newHttpCookie("test",TextBox1.Text);hc.Expires=DateTime.Now.AddYears(1);Response.Cookies.Add(hc);}else{Response.Cookies["test"].Value=TextBox1.Text;}
解决方案
解决方案二:
http://blog.csdn.net/ETstudio/archive/2007/10/09/1816463.aspx这个可以帮到你
解决方案三:
if(string.isNullOrEmpty(Request.Cookies["test"]))试试
解决方案四:
之前写的一个示例希望对你有所帮助<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="cookie.aspx.cs"Inherits="easy_cookie"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title><scriptlanguage="javascript">//根据名称和参数读取cookie值////functionGetCookie(name,params)//{//varoriginstr=document.cookie;//vartmp;//varreg=newRegExp("(?<=a=(\w+?=\w+?&)*?ip=)\w+(?=([&;])?)","gi");//tmp=reg.exec(originstr);//returntem;//}functionGetCookie(name){m=document.cookie;re1=newRegExp("(?!w)"+name+"=[^;]+","");re2=newRegExp("^"+name+"=","");try{vara=m.match(re1)[0];}catch(e){returnnull}eval("varo="+a.replace(re2,"{").replace(/&/g,"',").replace(/=/g,":'")+"'}");returno;}functionReadCookie(){varvar1=document.form1.txtName.value;document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP;}functionWriteCookie(){document.cookie=document.form1.txtName.value+"=IP="+document.form1.txtIP.value;}functionClearCookie(){document.cookie="";}</script></head><body><formid="form1"runat="server"><tableborder="0"cellpadding="0"cellspacing="0"width="778px"><tr><tdstyle="height:100px;width:778px;"align="center"valign="middle">Cookie示例(对IP的操作)</td></tr><tr><tdalign="center"style="width:778px"><br/> <asp:LabelID="Label1"runat="server"Text="名称"></asp:Label><asp:TextBoxID="txtName"runat="server"></asp:TextBox><br/> <asp:LabelID="Label2"runat="server"Text="IP"></asp:Label><asp:TextBoxID="txtIP"runat="server"></asp:TextBox><br/><asp:LabelID="Label3"runat="server"Text="读取IP"></asp:Label><asp:TextBoxID="txtReadIP"runat="server"></asp:TextBox><br/></td></tr><tr><tdalign="center"style="height:25px;width:778px;"><br/><br/><asp:ButtonID="btnRun"runat="server"Text="服务器设置Cookie"OnClick="Button1_Click"/><asp:ButtonID="btnGet"runat="server"Text="服务器读取Cookie"OnClick="Button2_Click"Width="158px"/> <asp:ButtonID="Button3"runat="server"Text="服务器清除Cookie"OnClick="Button3_Click"/><br/><buttontype="button"onclick="WriteCookie()">客户端设置Cookie</button> <buttontype="button"onclick="ReadCookie()">客户端读取Cookie</button> <buttontype="button"onclick="ClearCookie()">客户端清除Cookie</button><br/></td></tr></table></form></body></html>
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclasseasy_cookie:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}//写入protectedvoidButton1_Click(objectsender,EventArgse){if(Request.Cookies[txtName.Text]==null){HttpCookiecookie=newHttpCookie(txtName.Text);cookie["IP"]=txtIP.Text;Response.Cookies.Add(cookie);}else{Response.Cookies[txtName.Text]["IP"]=txtIP.Text;//Response.Write("<script>alert('已存在');</script>");}}//读取protectedvoidButton2_Click(objectsender,EventArgse){HttpCookiecookie=Request.Cookies[txtName.Text];if(cookie!=null){txtReadIP.Text=cookie["IP"];}}//清除cookieprotectedvoidButton3_Click(objectsender,EventArgse){for(inti=0;i<Request.Cookies.Count;i++){HttpCookiecookie=Request.Cookies[i];cookie.Expires=DateTime.Now.AddDays(-1);}//Response.Write(Request.Cookies.Count);//HttpCookiecookie=Request.Cookies;//cookie.Expires=DateTime.Now.Add(-1);}}
解决方案五:
学习了
解决方案六:
MARK!
解决方案七:
UP
解决方案八:
我没看出什么问题.等待高手解决
解决方案九:
Cookie有一系列的参数,在javascript中都可以操作。LZ的写法差不多,估计是某些参数的问题
解决方案十:
cookie有两种,一种存放于浏览器中,一种在硬盘上,存储于硬盘上的总是有效,存储于浏览器中的根据浏览器不同有不同的处理方式!存储在硬盘上的cookie可以在不同的浏览器进程间共享,比如两个IE窗口。而对于保存在内存里的cookie,不同的浏览器有不同的处理方式。对于IE,在一个打开的窗口上按Ctrl-N(或者从文件菜单)打开的窗口可以与原窗口共享,而使用其他方式新开的IE进程则不能共享已经打开的窗口的内存cookie;对于MozillaFirefox0.8,所有的进程和标签页都可以共享同样的cookie。一般来说是用javascript的window.open打开的窗口会与原窗口共享内存cookie。or:http://blog.csdn.net/lhminjava/archive/2007/11/07/1871202.aspx