public class LastProducts
{
#region 变量
private int _productid;
private int _categoryid;
private string _imgsrc = string.Empty;
private string _productname = string.Empty;
#endregion
#region 构造函数
public LastProducts() { }
public LastProducts(int id, int typeid, string imgsrc, string restorename)
{
_productid = id;
_categoryid = typeid;
_imgsrc = imgsrc;
_productname = restorename;
}
#endregion
#region 公共属性
public int Productid
{
get { return _productid; }
set { _productid = value; }
}
public int Categoryid
{
get { return _categoryid; }
set { _categoryid = value; }
}
public string Imgsrc
{
get { return _imgsrc; }
set { _imgsrc = value; }
}
public string Productname
{
get { return _productname; }
set { _productname = value; }
}
#endregion
}
//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为存储浏览记录数的数量显示4个
{
// 超过数量,去掉最先入队的元素
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)
{
LastProducts pro = new LastProducts(); //商品的实体类
pro.Productid = 999;
pro.Categoryid = 12;
pro.Imgsrc = "1231232144";
pro.Productname = "测试商品";
if (pro != null)
{
list.Add(new LastProducts(pro.Productid, pro.Categoryid, pro.Imgsrc, pro.Productname));
}
}
}
return list;
}
protected void Button1_Click(object sender, EventArgs e)
{
//4、在用户浏览某产品时记录到cookies中
HistoryRestore("restoreid", Convert.ToInt32(TextBox1.Text.Trim()));
}
protected void Page_Load(object sender, EventArgs e)
{
//5.数据源的绑定
Repeater1.DataSource = GetLastProducts();
Repeater1.DataBind();
}
.aspx页面
<div>
输入浏览的商品ID:<asp:TextBox ID="TextBox1" runat="server" Text="1"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="确定" οnclick="Button1_Click" />
<br />
<br />
<br />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
商品名称:<%#Eval("Productname")%> 商品ID:<%#Eval("Productid")%><br />
</ItemTemplate>
</asp:Repeater>
</div>