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>