asp.net 使用cookies或者session实现浏览历史记录功能
2013-01-03 20:51
721 查看
cooies实现方式:
/// <summary>
///HistoryRestore 的摘要说明
///最近浏览记录
/// </summary>
public class HistoryRestore
{
public HistoryRestore()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static void HistoryRestoreList(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 > 11) //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);
}
}
}
HistoryRestore.HistoryRestoreList("restoreid", int.Parse(pid));
http://www.cnblogs.com/woshilee/articles/2391722.html
使用session方式:
List<string> list = null;
if (Session["track"] == null)
{
list = new List<string>();
}
else
{
list = (List<string>)Session["track"];
}
if (list.Contains(pid))
list.Remove(pid); // 如果这次浏览的商品在浏览记录中,删除后重新添加进去,保持浏览顺序
if (list.Count == 10)
list.RemoveAt(0); // 浏览记录保存10个,到了10个删除最老的一个记录.
list.Add(pid); // 浏览顺序为list[9] list[8].....list[1] list[0]
Session["track"] = list; // 把list更新到Session
///////////////////////////////////////////////////////////////
if (list.Count > 0)
{
List<VCtuan.Model.Tb_Product> proList = new List<VCtuan.Model.Tb_Product>();
for (int i = list.Count - 1; i >= 0; i--)
{
string proId = list[i];
proList.Add(proBLL.GetModel(int.Parse(proId)));
}
RptLastProductList.DataSource = proList;
RptLastProductList.DataBind();
}
http://zhidao.baidu.com/question/199885679.html
读取cookies存储数据
/// <summary>
///HistoryRestore 的摘要说明
///最近浏览记录
/// </summary>
public class HistoryRestore
{
public HistoryRestore()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static void HistoryRestoreList(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 > 11) //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);
}
}
}
在用户浏览某产品时记录到cookies中:
HistoryRestore.HistoryRestoreList("restoreid", int.Parse(pid));
读取cookies存储数据
List<VCtuan.Model.Tb_Product> proList = null; if (Request.Cookies["restoreid"] != null) { HttpCookie tempCurBuyerList = Request.Cookies["restoreid"]; string[] strArr = tempCurBuyerList.Value.Split(','); proList = new List<VCtuan.Model.Tb_Product>(); foreach (string s in strArr) { VCtuan.Model.Tb_Product model = proBLL.GetModel(int.Parse(s)); //商品的实体类 if (model != null) { proList.Add(model); } } } RptLastProductList.DataSource = proList; RptLastProductList.DataBind();
http://www.cnblogs.com/woshilee/articles/2391722.html
使用session方式:
List<string> list = null;
if (Session["track"] == null)
{
list = new List<string>();
}
else
{
list = (List<string>)Session["track"];
}
if (list.Contains(pid))
list.Remove(pid); // 如果这次浏览的商品在浏览记录中,删除后重新添加进去,保持浏览顺序
if (list.Count == 10)
list.RemoveAt(0); // 浏览记录保存10个,到了10个删除最老的一个记录.
list.Add(pid); // 浏览顺序为list[9] list[8].....list[1] list[0]
Session["track"] = list; // 把list更新到Session
///////////////////////////////////////////////////////////////
if (list.Count > 0)
{
List<VCtuan.Model.Tb_Product> proList = new List<VCtuan.Model.Tb_Product>();
for (int i = list.Count - 1; i >= 0; i--)
{
string proId = list[i];
proList.Add(proBLL.GetModel(int.Parse(proId)));
}
RptLastProductList.DataSource = proList;
RptLastProductList.DataBind();
}
http://zhidao.baidu.com/question/199885679.html
相关文章推荐
- 使用cookies实现浏览历史记录功能
- 使用cookies实现浏览历史记录功能
- 使用cookies实现浏览历史记录功能
- 使用cookies实现浏览历史记录功能
- 在ASP.NET中使用IHttpHandler处理请求(如自实现AJAX)时,无法获得Session(或者说是Session 为 null)的原因及解决方法
- ASP.NET使用Cookie简单实现记住登陆状态功能
- 在ASP.NET中使用ObjectDataSource数据控件实现插入新记录
- asp.net使用jquery实现搜索框默认提示功能
- ASP.NET中使用Application对象实现简单在线人数统计功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- Asp.NET MVC中使用SignalR实现推送功能
- ASP.NET使用AspNetPager实现简单的分页功能
- asp.net中使用cookie与md5加密实现记住密码功能的实现代码
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- Asp.Net 自定义控件实现图片的上传,浏览,Delete功能 推荐
- 使用Cookies保存网站历史浏览记录实例代码
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- Asp.net网站使用HttpHandler实现图片防盗链功能
- ASP.NET之AdRotator实现淘宝浏览页面的商品随机推荐功能