怎么实现显示用户浏览过的历史记录
类别:编程学习 浏览量: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();
您可能感兴趣
- serv-u怎么用web登录(serv-u服务器的管理方法与功能分析)
- WEB API 中get、post、put,delete请求方式
- web接口测试中需要测试的几个点
- web安全漏洞怎么解决(Web服务器常见8种安全漏洞)
- webmaven项目如何启动(js基础语法与maven项目配置教程案例)
- webpack打包公共文件(webpack文件打包错误异常)
- 怎么创建dhcp服务器(DHCP服务器与Web服务器的搭建教程)
- webapp前端能用yii2开发吗(Yii Framework框架使用PHPExcel组件的方法示例)
- nginx配置两个前端web服务(你真的了解如何将Nginx配置为Web服务器吗)
- Web.Release.config 和 Web.Debug.config
- web服务器iis安全访问机制(IIS与APACHE实现HTTP重定向到HTTPS)
- 钉钉嵌入web端应用程序(钉钉企业内部H5微应用开发详解)
- typescript开发后端(教你使用webpack打包编译TypeScript代码)
- 最小web服务器linux系统(常用的web服务器软件整理win+linux)
- ASP.NET Web API中参数的传递
- websocket聊天室怎么设置(基于websocket实现简单聊天室对话)
- 职场人改不掉这4个习惯,只会越混越穷,一辈子也翻不了身(职场人改不掉这4个习惯)
- 华为 联想等46家公司笔试面试题,涉及各行各业,建议收藏(联想等46家公司笔试面试题)
- ()
- ()
- 800壮士拼死拖住30万日军 八佰 的真实历史,誓与阵地共存亡(800壮士拼死拖住30万日军)
- 演员陈创,火于 哮天犬 ,颠峰于 福贵 ,现状却令人唏嘘(演员陈创火于哮天犬)
热门推荐
- python中的多线程详解(python多线程抽象编程模型详解)
- JavaScript命名空间
- 怎么开放sql server端口(SQLServer2019配置端口号的实现)
- phpstudymysql启动又自动停止(Win下解决phpStudy MySQL启动失败)
- centos怎样开启tomcat(Centos8.2云服务器环境安装Tomcat8.5的详细教程)
- CSS设置字体
- dede高性能设置(让dedecms生成html速度快1倍)
- 小程序可滑动弧形进度条(小程序实现文字循环滚动动画)
- python经典算法(浅谈python常用程序算法)
- 使用ftp软件搭建ftp服务器(架设一个外网可以访问的FTP服务器图文教程)