您的位置:首页 > 编程语言 > ASP

asp.net 使用cookies或者session实现浏览历史记录功能

2013-01-03 20:51 721 查看
cooies实现方式:

读取cookies存储数据


/// <summary>
///HistoryRestore 的摘要说明
///最近浏览记录
/// </summary>
public class HistoryRestore
{
public HistoryRestore()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static void HistoryRestoreList(string cookieName, int objectID)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;

if (Request.Cookies[cookieName] != null)
{
HttpCookie tempCurBuyerList = Request.Cookies[cookieName];
string tempstr = tempCurBuyerList.Value;
if (tempstr.IndexOf(",") > 0)
{
string[] sArray = tempstr.Split(',');
bool hasthis = false;

foreach (string s in sArray)
{
if (s == objectID.ToString())
{
hasthis = true;
break;
}
else
{
hasthis = false;
}
}

if (!hasthis) //如果没有ID,则加入
{
if (sArray.Length > 11) //3为存储浏览记录数的数量,实际数量为7
{
// 超过数量,去掉最先入队的元素
tempstr = tempstr.Substring(0, tempstr.LastIndexOf(","));
}
// 队列
tempstr = objectID.ToString() + "," + tempstr;
}
}
else
{
//tempstr += "," + objectID.ToString();
if (tempstr != objectID.ToString())
{
tempstr = objectID.ToString() + "," + tempstr;
}
}
tempCurBuyerList.Value = tempstr;
tempCurBuyerList.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(tempCurBuyerList);
//或者 Response.Cookies[cookieName].Value = tempstr;
}
else
{
HttpCookie addToCookies = new HttpCookie(cookieName);
addToCookies.Value = objectID.ToString();
addToCookies.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(addToCookies);
}
}
}
在用户浏览某产品时记录到cookies中:


            HistoryRestore.HistoryRestoreList("restoreid", int.Parse(pid));

读取cookies存储数据


List<VCtuan.Model.Tb_Product> proList = null;
if (Request.Cookies["restoreid"] != null)
{
HttpCookie tempCurBuyerList = Request.Cookies["restoreid"];

string[] strArr = tempCurBuyerList.Value.Split(',');
proList = new List<VCtuan.Model.Tb_Product>();

foreach (string s in strArr)
{
VCtuan.Model.Tb_Product model = proBLL.GetModel(int.Parse(s)); //商品的实体类
if (model != null)
{
proList.Add(model);
}
}
}
RptLastProductList.DataSource = proList;
RptLastProductList.DataBind();

http://www.cnblogs.com/woshilee/articles/2391722.html
使用session方式:

List<string> list = null;
if (Session["track"] == null)
{
list = new List<string>();
}
else
{
list = (List<string>)Session["track"];
}

if (list.Contains(pid))
list.Remove(pid); // 如果这次浏览的商品在浏览记录中,删除后重新添加进去,保持浏览顺序
if (list.Count == 10)
list.RemoveAt(0); // 浏览记录保存10个,到了10个删除最老的一个记录.
list.Add(pid); // 浏览顺序为list[9] list[8].....list[1] list[0]
Session["track"] = list; // 把list更新到Session

///////////////////////////////////////////////////////////////
if (list.Count > 0)
{
List<VCtuan.Model.Tb_Product> proList = new List<VCtuan.Model.Tb_Product>();
for (int i = list.Count - 1; i >= 0; i--)
{
string proId = list[i];
proList.Add(proBLL.GetModel(int.Parse(proId)));
}
RptLastProductList.DataSource = proList;
RptLastProductList.DataBind();
}
http://zhidao.baidu.com/question/199885679.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: