怎么实现显示用户浏览过的历史记录
类别:编程学习 浏览量:2561
时间:2013-8-16 怎么实现显示用户浏览过的历史记录
怎么实现显示用户浏览过的历史记录主要的思路是:当用户浏览一个网页的时候写入cookie,再统一把cookie读出来就是历史记录了。其实现步骤为:
1.创建历史记录的实体类
public class LastProducts
{
private int _productid;
private int _categoryid;
private string _imgsrc;
private string _productname;
public LastProducts(int id,int typeid,string imgsrc,string restorename)
{
_productid = id;
_categoryid = typeid;
_imgsrc = imgsrc;
_productname = restorename;
}
public int ProductId
{
get { return _productid; }
}
public int CategoryId
{
get { return _categoryid; }
}
public string ImgSrc
{
get { return _imgsrc; }
}
public string ProductName
{
get { return _productname; }
}
}
2.定义存储cookies的方法
public void HistoryRestore(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 > 3) //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);
}
}
3.读取cookies存储数据
public List<LastProducts> GetLastProducts()
{
HttpRequest Request = HttpContext.Current.Request;
List<LastProducts> list = null;
if (Request.Cookies["restoreid"] != null)
{
HttpCookie tempCurBuyerList = Request.Cookies["restoreid"];
string[] strArr = tempCurBuyerList.Value.Split(',');
list = new List<LastProducts>();
foreach (string s in strArr)
{
ShopProduct model = dal.GetProById(int.Parse(s)); //商品的实体类
if (model != null)
{
list.Add(new Model.Shop.LastProducts(model.ProductID, model.CategoryID, model.ImageHref, model.Name));
}
}
}
return list;
}
4.在用户浏览某产品时记录到cookies中:
HistoryRestore("restoreid",productId);
5.数据源的绑定
Repeater1.DataSource = GetLastProducts();
Repeater1.DataBind();
您可能感兴趣
- 搭建web服务器与设置(怎样在Win7系统中搭建Web服务器)
- localstorage底层技术(浅谈Web Storage API的使用)
- webkit倒影方法(巧用 -webkit-box-reflect 倒影实现各类动效小结)
- 使用Fiddler测试WebApi接口
- pythonweb大数据分析(Python实现的大数据分析操作系统日志功能示例)
- 静态web服务器nginx(WEB服务器该选择 Apache 还是 Nginx?)
- web服务器与应用服务器的区别(web服务器是什么?web服务器有哪些呢?)
- webaccess签名设置(.htaccess文件写法之作用范围)
- react native web白屏(关于React Native 无法链接模拟器的问题)
- web界面测试中需要测试的几个点
- 前端webpack方面的优化(webpack的移动端适配方案小结)
- vueweb端聊天(Vue实现聊天界面)
- html5定位教学(Html5 webview元素定位工具的实现)
- web服务器软件的知识体系(七款主流WEB服务器软件点评)
- zabbix监控页面(Zabbix WEB监测实现过程图解)
- 数据库负载均衡的原理(理解web服务器和数据库的负载均衡以及反向代理)
- 秦海璐炫耀和王新军热恋蜜事,不料对方吐槽她吃饱后肚子撅老高(秦海璐炫耀和王新军热恋蜜事)
- 秦海璐一袭旗袍惹人倾心,将高级与淡雅展现的游刃有余(秦海璐一袭旗袍惹人倾心)
- 门外之见 海蛎子味 的表演,能走多远(门外之见海蛎子味)
- 三部冷门谍战剧,第一部2014年拍摄,至今还未播出(三部冷门谍战剧)
- 《金陵秘事》的剧情跌宕起伏 给观众带来的怎样的感官体验(金陵秘事的剧情跌宕起伏)
- 少儿口才表达影响未来一生,50首经典绕口令和孩子玩出聪明大脑(少儿口才表达影响未来一生)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9