日期:2014-05-17 浏览次数:20472 次
string value = string.Empty; if (HttpContext.Current.Request.Cookies["Mall_IsLogin"] != null) { value = HttpContext.Current.Request.Cookies["Mall_IsLogin"].Value; if (!string.IsNullOrEmpty(value)) { //需要执行的代码 }
protected void Button1_Click(object sender, EventArgs e) { HttpCookie objCookie = new HttpCookie("Mall_IsLogin"); objCookie.Value = "高丽佳33"; DateTime dtExpiry = DateTime.Now.AddDays(10); objCookie.Expires = dtExpiry; objCookie.Domain = ".h2h.cn"; if (this.Response.Cookies["Mall_IsLogin"] == null) { Response.Cookies.Add(objCookie); } else { this.Response.Cookies.Remove("Mall_IsLogin"); this.Response.Cookies.Set(objCookie); } } protected void Button2_Click(object sender, EventArgs e) { if (Request.Cookies["Mall_IsLogin"] != null) { string v = Request.Cookies["Mall_IsLogin"].Value; Button2.Text = v; } }
if (Request.Cookies["Mall_IsLogin"] != null) { string v = Request.Cookies["Mall_IsLogin"].Value; Button2.Text = v; }
------解决方案--------------------
你的测试代码存在两个问题
1.下面的代码永远不会执行,因为Response.Cookies["Mall_IsLogin"]!=null的
if (this.Response.Cookies["Mall_IsLogin"] == null) { Response.Cookies.Add(objCookie); }