asp.net 购物车 cookie版

地址来源:http://www.cnblogs.com/duwx/archive/2010/08/30/1812415.html

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

//在购物车的Cookies中查找是否存在这次要选择的物品
foreach (string item in hc.Values)
{
if (item == id)
{
flag = false;
break;
}
}

if (flag)
{
//如果选择的内容在购物车中没有,则创建一个新的子键
hc.Values.Add(id, id + "|" + name + "|" + scprice + "|" + hyprice + "|" + count + "|");
}
else
{
//如果选择的内容在购物车中,则删除原来的,添加一个新的
int num = int.Parse(hc.Values[id].Split(new char[] { '|' })[4]) + Convert.ToInt32(count);
hc.Values.Remove(id);
hc.Values.Add(id, id + "|" + name + "|" + scprice + "|" + hyprice + "|" + count);
}
hc.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(hc);
Response.Redirect("/ShoppingCar/");

 

posted @ 2016-02-23 16:11  西西里卡卡  阅读(185)  评论(0编辑  收藏  举报