asp.net用cookies添加浏览记录

//1、创建历史记录的实体类
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 />
        &nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="确定" οnclick="Button1_Click" />
        <br />
        <br />
        <br />
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
            商品名称:<%#Eval("Productname")%>&nbsp;&nbsp;商品ID:<%#Eval("Productid")%><br />
            </ItemTemplate>
        </asp:Repeater>
    </div>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值