VB.net
1. 首先定义存储cookies的对象:
Public Class ResortCookiesData
Private _Img As String
Private _resortName As String
Private _id As Integer
Public Sub New(img As String, resortname As String, id As Integer)
_Img = img
_resortName = resortname
_id = id
End Sub
Public ReadOnly Property Img() As String
Get
Return _Img
End Get
End Property
Public ReadOnly Property ResortName() As String
Get
Return _resortName
End Get
End Property
Public ReadOnly Property Id() As Integer
Get
Return _id
End Get
End Property
End Class
2. 读取cookies存储数据并绑定到数据控件中:
Protected Sub bindCookies()
If Page.Request.Cookies("resortid") IsNot Nothing Then
Dim _tempCurBuyerList As HttpCookie = Page.Request.Cookies("resortid")
Dim strArr As String() = _tempCurBuyerList.Value.Split(","C)
Dim list As New ArrayList()
For Each s As String In strArr
Dim m As SendNet.Model.Product = pm.Product.GetModel(Integer.Parse(s))
If m IsNot Nothing Then
list.Add(New ResortCookiesData(m.Img, m.ProductName, m.ProductId))
End If
Next
rpthistory.DataSource = list
rpthistory.DataBind()
End If
End Sub
3. 定义存储cookies的方法:
Protected Sub History_Resorts(_cookiesName As String, objectID As Integer)
If Page.Request.Cookies(_cookiesName) IsNot Nothing Then
Dim _tempCurBuyerList As HttpCookie = Page.Request.Cookies(_cookiesName)
Dim _tempstr As String = _tempCurBuyerList.Value
If _tempstr.IndexOf(",") > 0 Then
Dim sArray As String() = _tempstr.Split(","C)
Dim hasthis As Boolean = False
For Each i As String In sArray
If i = objectID.ToString() Then
hasthis = True
Exit For
Else
hasthis = False
End If
Next
If Not hasthis Then
'如果没有ID,则加入
If sArray.Length > 6 Then
'6为存储浏览记录数的数量,实际数量为7
'超过限定,去掉最先入队的元素
_tempstr = _tempstr.Substring(0, _tempstr.LastIndexOf(","))
End If
'队列
_tempstr = objectID.ToString() & "," & _tempstr
End If
Else
'_tempstr += "," + objectID.ToString();
If _tempstr <> objectID.ToString() Then
_tempstr = objectID.ToString() & "," & _tempstr
End If
End If
_tempCurBuyerList.Value = _tempstr
_tempCurBuyerList.Expires = DateTime.Now.AddDays(7)
Page.Response.Cookies.Add(_tempCurBuyerList)
Else
Dim _addToCookies As New HttpCookie(_cookiesName)
_addToCookies.Value = objectID.ToString()
_addToCookies.Expires = DateTime.Now.AddDays(7)
Page.Response.Cookies.Add(_addToCookies)
End If
End Sub
4. 在用户浏览某产品时记录到cookies中:
History_Resorts("resortid", m.ProductId)
C#代码:
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();