在网站中使用Cookie的简单例子
2010-06-23 17:30
323 查看
前台放一个TextBox、一个Label、一个Button(value="注销")
后台:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = this.TextBox1.Text;
HttpCookie coolie = new HttpCookie("UserName");
coolie.Value = name;
coolie.Expires = DateTime.Now.AddDays(7);
this.Response.Cookies.Add(coolie);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
//检查请求中的Cookie信息
HttpCookie cookie = this.Request.Cookies["UserName"];
if (cookie != null)
{
this.Label1.Text = string.Format("你的名字是:{0}", cookie.Value);
}
else
{
this.Label1.Text = "未知用户";
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
//通过创建Expires的过期时间来达到删除的目的
HttpCookie cookie = new HttpCookie("UserName");
cookie.Expires = new DateTime(1999, 1, 1);
this.Response.Cookies.Add(cookie);
}
}
后台:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = this.TextBox1.Text;
HttpCookie coolie = new HttpCookie("UserName");
coolie.Value = name;
coolie.Expires = DateTime.Now.AddDays(7);
this.Response.Cookies.Add(coolie);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
//检查请求中的Cookie信息
HttpCookie cookie = this.Request.Cookies["UserName"];
if (cookie != null)
{
this.Label1.Text = string.Format("你的名字是:{0}", cookie.Value);
}
else
{
this.Label1.Text = "未知用户";
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
//通过创建Expires的过期时间来达到删除的目的
HttpCookie cookie = new HttpCookie("UserName");
cookie.Expires = new DateTime(1999, 1, 1);
this.Response.Cookies.Add(cookie);
}
}
相关文章推荐
- pyton简单网络爬虫,aspx网站中form使用到了__VIEWSTATE、__EVENTVALIDATION、cookie来验证的提交
- 在网站中使用Session的简单例子
- 如何使用epoll? 一个C语言的简单例子 - asdfjkl210 - ITeye技术网站
- 如何使用epoll? 一个C语言的简单例子 - asdfjkl210 - ITeye技术网站
- SpringMVC 使用poi导出excel简单小例子
- okhttp的简单使用例子
- C#使用xpath简单爬取网站的内容
- 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)
- HTablePool简单使用例子
- 网站前端_EasyUI.基础入门.0001.jQuery EasyUI简单介绍与快速使用?
- python 使用 talib 和 numpy 的简单例子
- 牛客网Java刷题知识点之匿名对象、匿名对象的内存结构图、匿名对象的应用场景、匿名对象的使用、匿名对象的简单例子、匿名对象要注意的事项
- j2ee 简单网站搭建:(十一)ckeditor 控件使用入门
- 简单的AsyncTask使用例子
- Cookie的简单使用
- PHP中Cookie的使用详解(简单易懂)
- logback+slf4j日志使用简单入门例子
- Joda-Time简介与简单使用例子
- 使用 CXF 做 webservice 简单例子
- 使用class mailmessage 的简单例子.