将客户输入的姓名、性别的信息存储在cookie中,代码怎么写?

解决方案 »

  1.   



    HttpCookie ckName = new HttpCookie["name"];
    ckName.Value = "张三";
    Response.AppendCookie(ckName );HttpCookie ckSex= new HttpCookie["Sex"];
    ckSex.Value = "男人";
    Response.AppendCookie(ckSex);取
    string cookieName="";
    string cookieSex="";
    HttpCookie ckn= Request.Cookies["ckName"];
    cookieName= ckn.Value;
    HttpCookie cks= Request.Cookies["ckSex"];
    cookieSex=cks.Value;
      

  2.   

    HttpCookie ckName = new HttpCookie["name"];
    ckName.Value = "张三";
    Response.AppendCookie(ckName );难道对于每一个信息,我都要写这三行代码?
      

  3.   

    用多值Cookie:
    HttpCookie userCookie = new HttpCookie("UserInfo");
    userCookie.Values["UserName"] = "登录用户";
    userCookie.Values["UserMale"] = "男";
    Response.Cookies.Add(userCookie);
    读取时:
    if(Request.Cookies["UserInfo"]!=null)
    {
        string userName = Request.Cookies["UserInfo"].Values["UserName"];
        string userMale = Request.Cookies["UserInfo"].Values["UserMale"];
    }
    高级一点的,将用户信息封装为一个实体类,再将对用户信息的读写操作封装为一个属性或方法
    通过 HttpContext 类型读写,这样代码的可重用性高一些。
      

  4.   

    顶三楼的!如果信息多的话,可以先把信息存入HashTable中然后再存入Cookie中!
      

  5.   

    HttpCookie ckName = new HttpCookie["name"];
    ckName.Value = "张三";
    Response.AppendCookie(this.textbox1.text);HttpCookie ckSex= new HttpCookie["Sex"];
    ckSex.Value = "this.textbox2.text";
    Response.AppendCookie(ckSex);
      

  6.   

    System.Web.HttpCookie newcookie = new HttpCookie("user");
    newcookie.Values["username"] = "";
    newcookie.Values["sex"] = "";
    newcookie.Expires = DateTime.Now.AddDays(15);
    Response.AppendCookie(newcookie);