当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Cookie 的写入与读取

ASP.NET
ReferenceEquals, == , Equals 比较
VB.NET窗口渐淡关闭
使用简单的DepthBuffer 和使用Lights
VB下使用adodb.command 执行存储过程注意
Sendkeys 和 Sendmessage 使用技巧一例
使用IndexBuffer(索引)
探讨C#中字符串的加密
常用加密算法
如何更新父窗体
当SESSION失效时自动转到其它页面
Sendkeys 和 Sendmessage 使用技巧一例 选择自 northwolves 的 Blog
1.DotNet(.Net):新平台,C#:新语言
Web下打印的实现
在.NET中实现彩色光标,动画光标和自定义光标[引自孟子前辈作品]
alert在asp.net中如何使用??
替换HTML代码
h2reg的一些使用经验
C#2.0 新特性探究(一) 模拟List和内置算法
在网页中添加Flash的播放或者背景音乐
Cookie 的写入与读取

ASP.NET 中的 Cookie 的写入与读取


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 28 ::
收藏到网摘: n/a


private void SaveCookie(string CookieName,string CookieValue)
{
HttpCookie myCookie = new HttpCookie(CookieName);
DateTime now = DateTime.Now;
// Set the cookie value.
myCookie.Value = CookieValue;
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(1);
if(this.Response.Cookies[CookieName]!=null)
this.Response.Cookies.Remove(CookieName);
// Add the cookie.
this.Response.Cookies.Add(myCookie);
}
private string GetCookie(string CookieName)
{
HttpCookie myCookie = new HttpCookie(CookieName);
myCookie = Request.Cookies[CookieName];
// Read the cookie information and display it.
if (myCookie != null)
return myCookie.Value;
else
return null;
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();

base.OnInit(e);
}

///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
SaveCookie("mycookie","网际浪子");
}
private void Button2_Click(object sender, System.EventArgs e) { Response.Write(GetCookie("mycookie")); }