using system;
using system.web.ui;
using system.collections; //使用hashtable类必须引入该命名空间
namespace wendwcart //命名空间名称
{
[serializable]
public class stat_class{ //定义商品类,保存商品的各种属性
string shangpinid; //商品id
string sp_name; //商品名称
decimal sp_price; //商品价格
int sp_quan; //商品数量
public string itemid{
get{return shangpinid;}
set{shangpinid=value;}
}
public string shangpinname{
get{return sp_name;}
set{sp_name=value;}
}
public decimal price{
get{return sp_price;}
set{sp_price=value;}
}
public int quantity{
get{return sp_quan;}
set{sp_quan=value;}
}
public stat_class(string itemid,string shangpinname,decimal price,int quantity){ //构造方法,初始化商品的各个属性
shangpinid=itemid;
sp_name=shangpinname;
sp_price=price;
sp_quan=quantity;
}
}
[serializable]
public class shoppingcart{
hashtable cart_orders=new hashtable();
public icollection orders{
get{return cart_orders.values;}
}
public decimal totalcost{ //计算总价格
get{
decimal total=0;
foreach(dictionaryentry entry in cart_orders){
stat_class order=(stat_class)entry.value;
total+=(order.price*order.quantity);
}
return total;
}
}
public void additem(stat_class order){ //添加物件方法
stat_class order=( stat_class)cart_orders[order.itemid];
if(order!=null)
order.quantity+=order.quantity;
else
cart_orders.add(order.itemid,order);
}
public void deleteitem (string itemid){ //删除物件
if(cart_orders[itemid]!=null)
cart_orders.remove(itemid);
}
}
}
<%@ import namespace="wendwcart" %>
<%@ application codebehind="global.asax.cs" inherits="hdlab.bbs.global" %>
<script language="c#" runat="server">
void session_start()
{
session["myshoppingcart"]=new shoppingcart();
}
</script>
新闻热点
疑难解答
图片精选