前往顾页
以后地位: 主页 > 收集编程 > .Net实例教程 >

asp.net 实现 购物车 法度代码

时候:2010-08-08 02:12来源:知行网www.zhixing123.cn 编辑:麦田守望者

1 在构建商品传入的时候,建立session

2 session中安排一个实体类,来进行下个页面调用

3 购物车揭示页面,可以用gridview绑定model 来实现显现(操纵到键值)

shopping 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Tools
{
[Serializable]
//定义商品类,保存商品的各种属性
public class Stat_Class
{
private String ShangPinID; //商品ID
private String Sp_Name; //商品称呼
private decimal Sp_Price; //商品代价
private int Sp_Quan; //商品数量
private int Sp_Integral;

public string ItemID
{
get { return ShangPinID; }
//set { ShangPinID = value; }
}
public int SpIntegral
{
get { return Sp_Integral;}
}

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, int SpIntegral)
{
ShangPinID = ItemID;
Sp_Name = ShangpinName;
Sp_Price = Price;
Sp_Quan = Quantity;
Sp_Integral = SpIntegral;
}
}


[Serializable]
public class ShoppingCart
{
public ShoppingCart()
{
//
// TODO: 在此处增加机关函数逻辑
//
}
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 decimal TotalIntegral
{
get
{
decimal Total = 0;
foreach (DictionaryEntry entry in Cart_Orders)
{
Stat_Class order = (Stat_Class)entry.Value;
Total += (order.Quantity * order.SpIntegral);
}
return Total;
}
}

//增加物件体例
public void AddItem(Stat_Class Order)
{ //增加物件体例
Stat_Class order = (Stat_Class)Cart_Orders[Order.ItemID];
if (order != null)
order.Quantity += 1;
else
Cart_Orders.Add(Order.ItemID, Order);
}

//删除物件
public void DeleteItem(string ItemID)
{
if (Cart_Orders[ItemID] != null)
{
Cart_Orders.Remove(ItemID);
}
}
}
}

 

------分开线----------------------------
标签(Tag):ASP.NET asp.Net实例教程 ASP.NET教程 ASP.NET代码
------分开线----------------------------
保举内容
猜你感兴趣