西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)其它知識 → 編程實現(xiàn)cookies瀏覽歷史記錄功能

編程實現(xiàn)cookies瀏覽歷史記錄功能

前往專題相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2011/1/4 22:34:39字體大。A-A+

作者:佚名點擊:240次評論:0次標簽: cookies

cookies清除V4.1.8.8 綠色漢化版
  • 類型:卸載刪除大。1.3M語言:中文 評分:8.3
  • 標簽:
立即下載
 1.創(chuàng)建歷史記錄的實體類
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為存儲瀏覽記錄數(shù)的數(shù)量,實際數(shù)量為7
{
// 超過數(shù)量,去掉最先入隊的元素
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存儲數(shù)據(jù)
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.在用戶瀏覽某產(chǎn)品時記錄到cookies中:

HistoryRestore("restoreid",productId);


5.數(shù)據(jù)源的綁定
  Repeater1.DataSource = GetLastProducts();
Repeater1.DataBind();

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)