当页面加载的时候,COOKIE正常,能读出值,可当此页面中触发按钮事件的时候cookie的值就为空了,我设过此cookie的过期事件1年也没用,还请各位高手帮忙参详参详是什么原因。

解决方案 »

  1.   

    System.Web.HttpCookie usercookie = Request.Cookies["UserInfo"];
                if (usercookie != null)
                { 
                    string value=this.Request.Cookies["UserInfo"].Value.Tostring();
                }
                else
                {
                    Response.Redirect("../xx.aspx");
                }
    结果是value值为null
      

  2.   

                        string Enstr = System.Web.Security.FormsAuthentication.Encrypt(tk);
                        System.Web.HttpCookie ck = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, Enstr);
                        ck.Path = System.Web.Security.FormsAuthentication.FormsCookiePath;
                        ck.Expires = System.DateTime.Now.AddHours(8);
                        Response.Cookies.Add(ck);
      

  3.   

    Try It!        if (Request.Cookies["UserInfo"]!= null)
            {
                string value = Request.Cookies["UserInfo"];
                this.Label1.Text = value;
            }
            else {
                this.Label1.Text = "x";
            }
      

  4.   

    你的VALUE是局部变量执行出去就NULL了,不是你的COOKIE null 啊
      

  5.   

    string   value=this.Request.Cookies["UserInfo"].Value.Tostring(); ///???request
    只是在页面加载时可以得到Request,否则你点击按钮时,是执行回发,没有Request.所以可以用viewstate储存(在页面加载生命周期阶段)
      

  6.   

    userwjg我调试的时候那value就是null....
      

  7.   

    only_endure,那个viewstate存储怎么用的,麻烦给个例子
      

  8.   

    string value = Request.Cookies["UserInfo"];
    cookie to string?
      

  9.   


    if(HttpContext.Current.Request.Cookies["UserInfo"]!=null)
    string Str=HttpContext.Current.Request.Cookies["UserInfo"].Value
    不要使用value做变量名
      

  10.   

    不好意思,我的代码用的是
    Request.Cookies["UserInfo"]["abc"];
      

  11.   

    我这只是为了简便这么写而已,程序中不是VALUE。
      

  12.   

    你的应该是用 
    Request.Cookies["UserInfo"].Value.Tostring(); 
      

  13.   

    老七,按你那样写结果一样还是null
      

  14.   

    HttpContext.Current.Request.Cookies.Clear();
    this.Response.Cookies["UserInfo"].Value = "XX";
    this.Response.Cookies["UserInfo"].Expires = DateTime.Now.AddHours(1);
    其他页面都对的,就这么个页面丢cookie晕了
      

  15.   

    HttpCookie cookie = new HttpCookie("XXX");
    cookie.Value = yourvalue
    cookie.Expires = System.DateTime.Now.AddDays(yourdate);
    Response.Cookies.Add(cookie);
      

  16.   

    HttpContext.Current.Request.Cookies.Clear(); 
    this.Response.Cookies["UserInfo"].Value   =   "XX"; 
    this.Response.Cookies["UserInfo"].Expires   =   DateTime.Now.AddHours(1); 
    写在哪的,HttpContext.Current.Request.Cookies.Clear(); 是清空啊,还是页面周期问题你跟着事件梳理一下cookie的变化.
      

  17.   

    添加写在登录页面,然后首页也正常,但是再跳转个页面的时候page_load的时候还正常,一按按钮cookie就空
    了,事件里我也没清COOKIE啊
      

  18.   

    我现在找到替代的方法了,可是想知道是什么原因造成的cookie为null
      

  19.   

    看我12楼的说明,value是保留名
      

  20.   

    说了我实际程序中没用value。。
      

  21.   

    添加写在登录页面,然后首页也正常,但是再跳转个页面的时候page_load的时候还正常,一按按钮cookie就空 
    了,事件里我也没清COOKIE啊

    你点击按钮首先执行的是pageload,因此被清空,又没有赋值(回发情况下request从何而来)
    所以你那一段代码要写在if(!ispostback)里面限制一下...
      

  22.   

    告诉你个测试方法,在前台放一label,按钮事件里把其它逻辑注释,只写this.label.text=Request.Cookies["UserInfo"].Value看看,如果点击按钮后label没显示,再换成this.label.text=Request.Cookies["UserInfo"].Value.GetType().ToString()看看输出是什么类型,如果真是null说明你那个cookie根本不存在,思路就是这样,一点一点试么
      

  23.   

    那不成了只有if(!ispostback)中能拿到cookie了?
    搞清楚就结贴了
      

  24.   

    cookie是保留的啊,我说清空与赋值在第一次加载页面时.
      

  25.   

    这个我知道啊!你不是说回发没request嘛?那回发怎么拿cookie?
      

  26.   

    抓个包看下
    看下包头有没有cookies传过去
    没得话代码是永远读不到的