购物车代码
时间:2011-03-29 来源:志神Soft
<1>购物车泛型集合实现
public Dictionary<int,CartType> ShoppingCart
{
get
{
if(Session["cart"]==null){
return null;
}else
{
return Session["cart"] as Dictionary<int,CaryType>;
}
set
{
Session["cart"]=value;
}
}
<2>点购买事件
CartType c=new CartType();
c.ID=0;
c.Name="ds";
c.Sum=1;
if(ShoppingCart==null)
{
Dictionnary<int,CartType> t=new Dictionary<int,CartType>();
t.Add(c.ID,c):
ShangppingCart=t;
} else
{
Dictionary<int,CartType> t2=ShangppingCart;
if(t2.ContainesKey(c.ID)){
t2[c.ID].Sum++;
}else
{
t2.Add(c.ID,c);
}
}
<3>购物车页面
if(ShangppingCart==null){
return;
}
GridView1.DataKeyName=new string[]{"ID"};
GridView1.DataSource=ShangpingCart.Value;
GridView1.DataBind();
<4>删除购物车商品
int id=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
ShangpingCart.Remove(id);