if (this.chkRemember.Checked == true)//前台页中保存两周复选框勾选
                        {
                            HttpCookie cookie = new HttpCookie("hluserinfo");                            cookie.Values.Add("hluserid", dr["userID"].ToString());
                            cookie.Values.Add("hlusername", dr["username"].ToString());
                            cookie.Values.Add("hluserquanxian", dr["userquanxian"].ToString());
                            cookie.Expires = DateTime.Now.AddDays(14);//14天有效期
                            Response.Cookies.Add(cookie);                        }
登录成功跳转去的页面后台:
        int userid;
        string username;
        int userquanxian;        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                HttpCookie mycookies = HttpContext.Current.Request.Cookies["hluserinfo"];
                
                if (mycookies != null) 
                {
                     userid = Convert.ToInt32(Response.Cookies["hluserinfo"].Values["hluserid"]);
                     username = Response.Cookies["hluserinfo"].Values["hlusername"];
                     userquanxian = Convert.ToInt32(Response.Cookies["hluserinfo"].Values["hluserquanxian"]);
                }                Response.Write(username);
            }
        }问题是,Response.Write(username);没有任何值,空的,,,,,,,请问,,,代码有没有写错?如果错了怎么改?大侠人帮帮我.......

解决方案 »

  1.   

    你打断点调试一下,估计COOkie为空了,获取不到值
      

  2.   

    不明白,前面辛辛苦苦的定义了
     HttpCookie mycookies = HttpContext.Current.Request.Cookies["hluserinfo"];
    并且做了验证,
    后面为什么不用,
    却用Response.Cookies。。
      

  3.   

    可是我执行写进cookie值了啊,,,上面代码有问题吗?
      

  4.   

    保存cookie用这两个
    Response.AppendCookie(cookie);
    Response.SetCookie(cookie);
      

  5.   

    刚好在做COOIKE的东西
    存入
     HttpCookie mycookie1 = new HttpCookie("abc");
                                System.Text.Encoding enc = System.Text.Encoding.GetEncoding("gb2312");                            mycookie1.Values.Add("userName", HttpUtility.UrlEncode(userName.Text, enc));
                                mycookie1.Values.Add("passWord", HttpUtility.UrlEncode(passWord.Text, enc));
                                                               mycookie1.Expires = System.DateTime.Now.AddDays(365);
                                                           Response.Cookies.Add(mycookie1);读取 System.Text.Encoding enc = System.Text.Encoding.GetEncoding("gb2312");
                        userName.Text = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["abc"].Values["userName"].ToString(),enc);
                        string pass =  HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["abc"].Values["passWord"].ToString(),enc);
      

  6.   

    foren_whb老大,你真是一语惊醒梦中人啊,,,,,,就是这个原因,,搞定了
      

  7.   

    username = Response.Cookies["hluserinfo"].Values["hlusername"];
    换成username = Request.Cookies["hluserinfo"].Values["hlusername"];楼主你先理清一下Response对象与Request对象的区别