ASP.NET Cookie的使用汇总
1、解决Cookie更新滞后的问题
先写入一个过期的Cookie,再添加一个新的Cookie就OK了。示例代码如下所示:
protected void btnSearch_Click(object sender, EventArgs e)
{
//生成条件表达式
string where = bll.GetWhereSql(Int32.Parse(ddlCate.SelectedValue),
Int32.Parse(ddlHarbor.SelectedValue), 0, 0, ddlState.SelectedValue, tbKeyword.Text.Trim());
//判断条件Cookie是否为空,如不为空,则移除现有条件Cookie
if (Request.Cookies["Admin_HarborSpot_Where"] != null)
{
RemoveWhereCookie(where);//移除现有条件Cookie
}
//设置一个新的条件Cookie
SetWhereCookie(where);
BindData(true);
}

//设置条件Cookie
private void SetWhereCookie(string where)
{
HttpCookie cookie = new HttpCookie("Admin_HarborSpot_Where");
cookie.Value = where;
cookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(cookie);
}

//移除现存的条件Cookie
private void RemoveWhereCookie(string where)
{
HttpCookie cookie = Request.Cookies["Admin_HarborSpot_Where"];
cookie.Value = where;
cookie.Expires = DateTime.Now.AddDays(-3d);
Response.Cookies.Add(cookie);
}
先写入一个过期的Cookie,再添加一个新的Cookie就OK了。示例代码如下所示:
protected void btnSearch_Click(object sender, EventArgs e)
{
//生成条件表达式
string where = bll.GetWhereSql(Int32.Parse(ddlCate.SelectedValue),
Int32.Parse(ddlHarbor.SelectedValue), 0, 0, ddlState.SelectedValue, tbKeyword.Text.Trim());
//判断条件Cookie是否为空,如不为空,则移除现有条件Cookie
if (Request.Cookies["Admin_HarborSpot_Where"] != null)
{
RemoveWhereCookie(where);//移除现有条件Cookie
}
//设置一个新的条件Cookie
SetWhereCookie(where);
BindData(true);
}
//设置条件Cookie
private void SetWhereCookie(string where)
{
HttpCookie cookie = new HttpCookie("Admin_HarborSpot_Where");
cookie.Value = where;
cookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(cookie);
}
//移除现存的条件Cookie
private void RemoveWhereCookie(string where)
{
HttpCookie cookie = Request.Cookies["Admin_HarborSpot_Where"];
cookie.Value = where;
cookie.Expires = DateTime.Now.AddDays(-3d);
Response.Cookies.Add(cookie);
}posted on 2008-06-02 18:09 【好好学习天天向上】 阅读(1213) 评论(0) 收藏 举报

浙公网安备 33010602011771号