文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>asp.net使用Cookies实现购物车

asp.net使用Cookies实现购物车

时间:2010-08-30  来源:超越自我~_~!!

asp.net使用Cookies实现购物车

ListInfo.aspx向购物车的添加商品的方法

private void GouWu(string name, double price, string id)
    {
        //往购物车中添加商品
        HttpCookie hc = null;
        if (Request.Cookies["MyGouWuChe1"] == null)
        {
            //如果Cookies中不存在MyGouWuChe1,则创建
            hc = new HttpCookie("MyGouWuChe1");   
        }
        else
        {
            //如果Cookies中存在MyGouWuChe1,则取出
            hc= Request.Cookies["MyGouWuChe1"];
          
        }
        bool flag=true;//标记在购物车中是否存在本次选择的物品

        //在购物车的Cookies中查找是否存在这次要选择的物品
        foreach (string item in hc.Values)
        {
            if (item == id)
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            //如果选择的内容在购物车中没有,则创建一个新的子键
            hc.Values.Add(id, id + "|" + name + "|" + price + "|" + 1 + "|");     
        }
        else
        {
            //如果选择的内容在购物车中没,则删除原来的,添加一个新的
            int num = int.Parse(hc.Values[id].Split(new char[] { '|' })[3]) + 1;
            hc.Values.Remove(id);
            hc.Values.Add(id,id + "|" + name + "|" + price + "|" + num + "|");
        }
        hc.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(hc);
        Response.Redirect("gouWuChe.aspx");
    }

在gouWuChe.aspx页面的Load事件中

 List<GuoWuChe> list = new List<GuoWuChe>();
        //循环从购物车中取出物品添加到集合
        foreach (string item in Request.Cookies["MyGouWuChe1"].Values)
        {
            if (item != null)
            {
                char[] sp = { '|' };

                string[] w = Request.Cookies["MyGouWuChe1"][item].Split(sp);

                GuoWuChe gwc = new GuoWuChe();
                gwc.Id = w[0];
                gwc.Name = w[1];
                gwc.Price = int.Parse(w[2]);
                gwc.Number = int.Parse(w[3]);
                list.Add(gwc);
            }
        }
        GridView1.DataSource = list;
        GridView1.DataBind();

 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载