• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

编程实现cookies浏览历史记录功能

时间:2015年04月02日 15:38:39    来源:魔法猪系统重装大师官网    人气:7725
前往ie浏览器专题
 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 GetLastProducts()
{
HttpRequest Request = HttpContext.Current.Request;

List list = null;

if (Request.Cookies["restoreid"] != null)
{
HttpCookie tempCurBuyerList = Request.Cookies["restoreid"];

string[] strArr = tempCurBuyerList.Value.Split(',');
list = new List();

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();
编程,实现,cookies,浏览,历史,记录,功能,前往,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部