asp.net项目中如何运用 cookie保存文本浏览记录
1个回答
展开全部
当页面初始化时需要判断是否存在
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["username"] == null)
{
HttpCookie myCookie = new HttpCookie("username");
myCookie.Values.Add("prodid", "");
myCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(myCookie);
}
}
这是插入cookie的代码
public void addcookie(string cookievalue)
{
HttpCookie cookie = new HttpCookie("username");
if (Request.Cookies["username"].Values["prodid"] != "")
{
string myvalue = "";
myvalue = Request.Cookies["username"].Values["prodid"].ToString();
myvalue = cookievalue + "," + myvalue;
cookie.Values.Add("prodid", myvalue);
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
else
{
cookie.Values.Add("prodid", cookievalue);
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
}
读取cookie代码,只读取最后三条记录
public void playcookie()
{
int num = 0;
string myvalue = "";
myvalue = Request.Cookies["username"].Values["prodid"].ToString();
string resultvalue=string.Empty;
if (myvalue != "")
{
Char[] spitchar = new Char[] { Convert.ToChar(",") };
string[] cookievalue;
cookievalue = myvalue.Split(spitchar);
if (cookievalue.Length <= 3)
{
num = cookievalue.Length;
}
else
{
num = 3;
}
for (int i = 0; i < num; i++)
{
resultvalue += cookievalue[i].ToString();
}
Label1.Text = resultvalue;
}
}
最后是删除cookie的方法
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie("username");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["username"] == null)
{
HttpCookie myCookie = new HttpCookie("username");
myCookie.Values.Add("prodid", "");
myCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(myCookie);
}
}
这是插入cookie的代码
public void addcookie(string cookievalue)
{
HttpCookie cookie = new HttpCookie("username");
if (Request.Cookies["username"].Values["prodid"] != "")
{
string myvalue = "";
myvalue = Request.Cookies["username"].Values["prodid"].ToString();
myvalue = cookievalue + "," + myvalue;
cookie.Values.Add("prodid", myvalue);
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
else
{
cookie.Values.Add("prodid", cookievalue);
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
}
读取cookie代码,只读取最后三条记录
public void playcookie()
{
int num = 0;
string myvalue = "";
myvalue = Request.Cookies["username"].Values["prodid"].ToString();
string resultvalue=string.Empty;
if (myvalue != "")
{
Char[] spitchar = new Char[] { Convert.ToChar(",") };
string[] cookievalue;
cookievalue = myvalue.Split(spitchar);
if (cookievalue.Length <= 3)
{
num = cookievalue.Length;
}
else
{
num = 3;
}
for (int i = 0; i < num; i++)
{
resultvalue += cookievalue[i].ToString();
}
Label1.Text = resultvalue;
}
}
最后是删除cookie的方法
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie("username");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询