cookie点滴(一)

作者在 2007-05-17 23:38:00 发布以下内容

一、创建、读取具有多个键值对的cookie

1、创建

   (1)HttpCookie hc=new HttpCookie("visitor");
          hc.Values["username"]=this.TextBox1.Text;
          hc.Values["userpassword"]=this.TextBox2.Text;
          hc.Expires=DateTime.Now.AddDays(1);
   (2)Response.Cookies.Add(hc);
          Response.Cookies["visitor"]["username"]=this.TextBox1.Text;
          Response.Cookies["visitor"]["password"]=this.TextBox2.Text;
          Response.Cookies["visitor"].Expires=DateTime.Now.AddDays(1);

2、读取

    (1)

          HttpCookie hc=Request.Cookies["visitor"];

          if(hc.haskeys)

           {Response.Write("the username :"+hc.Values["username"].ToString());
           Response.Write("the password :"+hc.Values["userpassword"].ToString());

           }
   (2)if(Request.Cookies["visitor"].HasKeys)
   {
    System.Collections.Specialized.NameValueCollection userinfo=new System.Collections.Specialized.NameValueCollection();
    userinfo=Request.Cookies["visitor"].Values;
    Response.Write("the username :"+userinfo["username"].ToString());
    Response.Write("the password :"+userinfo["userpassword"].ToString());
   }

 

ASP.NET | 阅读 1802 次
文章评论,共0条
游客请输入验证码
浏览78236次