使用cookies实现浏览历史记录功能
2010-10-12 16:22
816 查看
1. 首先定义存储cookies的对象:
2. 读取cookies存储数据并绑定到数据控件中:
3. 定义存储cookies的方法:
4. 在用户浏览某产品时记录到cookies中:
(此功能到此完成)
public class ResortCookiesData { private string _Img; private string _resortName; private int _id; public ResortCookiesData(string img, string resortname, int id) { _Img = img; _resortName = resortname; _id = id; } public string Img { get { return _Img; } } public string ResortName { get { return _resortName; } } public int Id { get { return _id; } } }
2. 读取cookies存储数据并绑定到数据控件中:
protected void bindCookies() { if (Page.Request.Cookies["resortid"] != null) { HttpCookie _tempCurBuyerList = Page.Request.Cookies["resortid"]; string[] strArr = _tempCurBuyerList.Value.Split(','); ArrayList list = new ArrayList(); foreach (string s in strArr) { SendNet.Model.Product m = pm.Product.GetModel(int.Parse(s)); if (m != null) { list.Add(new ResortCookiesData(m.Img, m.ProductName, m.ProductId)); } } rpthistory.DataSource = list; rpthistory.DataBind(); } }
3. 定义存储cookies的方法:
protected void History_Resorts(string _cookiesName, int objectID) { if (Page.Request.Cookies[_cookiesName] != null) { HttpCookie _tempCurBuyerList = Page.Request.Cookies[_cookiesName]; string _tempstr = _tempCurBuyerList.Value; if (_tempstr.IndexOf(",") > 0) { string[] sArray = _tempstr.Split(','); bool hasthis = false; foreach (string i in sArray) { if (i == objectID.ToString()) { hasthis = true; break; } else { hasthis = false; } } if (!hasthis)//如果没有ID,则加入 { if (sArray.Length > 6)//6为存储浏览记录数的数量,实际数量为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(7); Page.Response.Cookies.Add(_tempCurBuyerList); } else { HttpCookie _addToCookies = new HttpCookie(_cookiesName); _addToCookies.Value = objectID.ToString(); _addToCookies.Expires = DateTime.Now.AddDays(7); Page.Response.Cookies.Add(_addToCookies); } }
4. 在用户浏览某产品时记录到cookies中:
History_Resorts("resortid", m.ProductId);
(此功能到此完成)
相关文章推荐
- 使用cookies实现浏览历史记录功能
- 使用cookies实现浏览历史记录功能
- 使用cookies实现浏览历史记录功能
- asp.net 使用cookies或者session实现浏览历史记录功能
- 使用EditText和SharedPreferences实现搜索历史记录提示功能
- 使用cookie实现打印浏览记录的功能
- 使用Cookies保存网站历史浏览记录实例
- javascript结合Cookies实现浏览记录历史第1/3页
- 使用Cookies保存网站历史浏览记录实例代码
- 使用Cookies保存网站历史浏览记录实例代码
- 用JS实现浏览历史记录功能
- 使用Cookie实现商品的浏览记录功能
- javascript结合Cookies实现浏览记录历史第1/3页
- 使用Android GPS api 实现一个轨迹记录的小功能
- python【7】实现历史的记录功能
- 使用Cookie实现保存商品浏览记录
- jquery实现搜索框历史搜索记录功能
- 为在telnet自己TCP服务器程序的界面上实现shell一样的自动补齐和历史记录的功能。
- 模拟亚马逊、淘宝等浏览记录(历史记录)功能(访问集合存储的数据)
- 使用cookie实现记录浏览商品的过程并能够清空浏览记录(简单的小程序不涉及到数据库的调取)