CSDN博客

img Kestin

Asp.Net Cookie 学习

发表于2004/10/29 10:04:00  1396人阅读

分类: .Net

近日对Asp.net的Cookie使用方法学习了一下
Response.Cookies    服务器传给客户端的Cookie(比如给客户端增加一个Cookie)
Request.Cookies      客户端传给服务器的Cookie

private void Button1_Click(object sender, System.EventArgs e)
  {
HttpCookie Cookie  = new HttpCookie("MyTest");
//Cookie.Expires = DateTime.Now.AddDays(1);
Cookie.Values.Add("name",TextBox1.Text);//Cookie里的值。可通过索引器访问
Cookie.Values.Add("pw",TextBox2.Text);
Cookie.Value = "HelloWorld";  //Cookie的单值
Response.Cookies.Add(Cookie);  //添加Cookie到Response写给客户端
Response.Write("Write Cookie To Client Suc");
}

private void Button2_Click(object sender, System.EventArgs e)
  {
   HttpCookie Cookie = Request.Cookies["MyTest"];//从Request里取得Cookie
   if(Cookie == null)
   {
    Response.Write("Client Haven't Cookie of This Web"); //Request里没有Cookie
    return;
   }
   TextBox3.Text = Cookie.Values["name"];//从Cookie里取值
   TextBox4.Text = Cookie.Values["pw"];
  TextBox5.Text = Cookie.Value;

  }

0 0

相关博文

我的热门文章

img
取 消
img