使用cookies实现浏览历史记录功能
2012-03-26 09:56
761 查看
使用cookies实现浏览历史记录功能
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();
原文地址:http://www.cnblogs.com/xgt2009/archive/2011/01/04/1925862.html
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();
原文地址:http://www.cnblogs.com/xgt2009/archive/2011/01/04/1925862.html
相关文章推荐
- 使用cookies实现浏览历史记录功能
- 使用cookies实现浏览历史记录功能
- asp.net 使用cookies或者session实现浏览历史记录功能
- 使用cookies实现浏览历史记录功能
- 使用EditText和SharedPreferences实现搜索历史记录提示功能
- javascript结合Cookies实现浏览记录历史第1/3页
- 使用Cookies保存网站历史浏览记录实例代码
- 使用Cookies保存网站历史浏览记录实例代码
- 使用cookie实现打印浏览记录的功能
- 使用Cookies保存网站历史浏览记录实例
- javascript结合Cookies实现浏览记录历史第1/3页
- 用JS实现浏览历史记录功能
- 使用Cookie实现商品的浏览记录功能
- 一种历史浏览功能的实现(C#)
- Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- js操作Cookie,实现历史浏览记录
- 使用cookie实现记录浏览商品的过程并能够清空浏览记录(简单的小程序不涉及到数据库的调取)
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- jquery使用Cookie和JSON记录用户最近浏览历史
- 使用localStorage替代cookie做本地存储,例如最近浏览历史记录