我以前的 购物车的时候 有时候也是这样的点一下 有时候买了一个 有时候买了两个我是放在GridView1_SelectedIndexChanged事件里面的 看到你也有这个问题所以也提出来 程序真的没有问题! void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Session["MyShoppingCart"] = new WebShoppingCart();
//读取当前统计人数
Application.Lock();
Application["Count"] = Convert.ToInt32(Application["Count"]) + 1;
Application.UnLock();
//写入Count.txt文件中
StreamWriter sw = new StreamWriter(Server.MapPath("Count.txt"), false);
sw.WriteLine(Application["Count"]);
sw.Close();
}protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//创建实例
WebShoppingCart cart = (WebShoppingCart)Session["MyShoppingCart"];
try
{
string pid = GridView1.SelectedDataKey.Value.ToString();
if (cart != null)
{
cart.DeleteItem(pid);
Profile.ShoppingCart.DeleteItem(pid);
Total.Text = String.Format("合计:{0:c}", cart.TotalCost);
//删除后的绑定,下面可以不要因为Page_Load事件绑定了
//ShoppingCart cart = (ShoppingCart)Session["MyShoppingCart"];
//GridView1.DataSource = cart.Orders;
GridView1.DataBind();
}
}
catch
{
//这个是为了判断用户删除了商品
//但是点击后退按钮后商品还在
//在点击删除出现得异常,用刷新该页来解决
Response.Redirect("View_ShoppingCart.aspx");
}
}类namespace WendwCart //命名空间名称
{
[Serializable]
public class Stat_Class
{
private String ShangPinID; //商品ID
private String Sp_Name; //商品名称
private decimal Sp_Price; //商品价格
private 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 WebShoppingCart
{
public WebShoppingCart()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
Hashtable Cart_Orders = new Hashtable();
//该属性返回Hashtable的ICollection集合作数据源
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 += 1;
}
else
{
Cart_Orders.Add(Order.ItemID, Order);
}
} //删除物件方法
public void DeleteItem(string ItemID)
{
if (Cart_Orders[ItemID] != null)
{
Cart_Orders.Remove(ItemID);
}
}
}
{
// 在新会话启动时运行的代码
Session["MyShoppingCart"] = new WebShoppingCart();
//读取当前统计人数
Application.Lock();
Application["Count"] = Convert.ToInt32(Application["Count"]) + 1;
Application.UnLock();
//写入Count.txt文件中
StreamWriter sw = new StreamWriter(Server.MapPath("Count.txt"), false);
sw.WriteLine(Application["Count"]);
sw.Close();
}protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//创建实例
WebShoppingCart cart = (WebShoppingCart)Session["MyShoppingCart"];
try
{
string pid = GridView1.SelectedDataKey.Value.ToString();
if (cart != null)
{
cart.DeleteItem(pid);
Profile.ShoppingCart.DeleteItem(pid);
Total.Text = String.Format("合计:{0:c}", cart.TotalCost);
//删除后的绑定,下面可以不要因为Page_Load事件绑定了
//ShoppingCart cart = (ShoppingCart)Session["MyShoppingCart"];
//GridView1.DataSource = cart.Orders;
GridView1.DataBind();
}
}
catch
{
//这个是为了判断用户删除了商品
//但是点击后退按钮后商品还在
//在点击删除出现得异常,用刷新该页来解决
Response.Redirect("View_ShoppingCart.aspx");
}
}类namespace WendwCart //命名空间名称
{
[Serializable]
public class Stat_Class
{
private String ShangPinID; //商品ID
private String Sp_Name; //商品名称
private decimal Sp_Price; //商品价格
private 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 WebShoppingCart
{
public WebShoppingCart()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
Hashtable Cart_Orders = new Hashtable();
//该属性返回Hashtable的ICollection集合作数据源
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 += 1;
}
else
{
Cart_Orders.Add(Order.ItemID, Order);
}
} //删除物件方法
public void DeleteItem(string ItemID)
{
if (Cart_Orders[ItemID] != null)
{
Cart_Orders.Remove(ItemID);
}
}
}
iframe 左边的菜单想 改一个名字 才出来是我的VS2005有问题 还是Bug啊??