本文介绍了ShoppingCart中的问题更新数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更新产品后产品数量不正确.
例如:选择产品001->数量:1
产品002->添加002,数量:1
产品002-> 001的数量:2 和002的数量:1
我的ShopCart课程:
The quantity of products incorrect after update the product.
Ex:select Product 001->quantity:1
Product 002->add 002,quantity:1
Product 002->001''s quantity:2 and 002''s quantity:1
My ShopCart class:
DataView dv=new DataView();
int rIndex=0;
DataRow dr;
public DataTable MakeCart()
{
DataTable tb = new DataTable("ShopCart");
tb.Columns.Add("ProductID");
tb.Columns.Add("quantity");
tb.Columns.Add("price");
tb.Columns.Add("total");
return tb;
}
public DataTable AddCart(string ProductID,int price)
{
DataTable tbCart = new DataTable();
tbCart = (DataTable)HttpContext.Current.Session["Cart"];
if (tbCart == null)
{
tbCart = MakeCart();
}
dv = tbCart.DefaultView;
dv.Sort = "ProductID";
rIndex = dv.Find(ProductID);
if (rIndex != -1)
{
tbCart.Rows[rIndex][1] = Convert.ToInt32(tbCart.Rows[rIndex][1]) + 1;
tbCart.Rows[rIndex][3] = Convert.ToInt32(tbCart.Rows[rIndex][1]) * Convert.ToInt32(tbCart.Rows[rIndex][2]);
}
else
{
dr = tbCart.NewRow();
dr[0] = ProductID;
dr[1] = 1;
dr[2] = price;
dr[3] = price;
//tbCart.AcceptChanges();
tbCart.Rows.Add(dr);
}
return tbCart;
}
单击添加按钮时发生的事件:
event when click Add button:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
DataTable tb = new DataTable();
string name = e.CommandName;
if (name == "Add")
{
Label IDP = (Label)e.Item.FindControl("lblProductID");
IDProduct = IDP.Text;
tb = scart.AddCart(IDProduct, 20000);
}
Session["Cart"] = tb;
Response.Redirect("Cart.aspx");
}
推荐答案
这篇关于ShoppingCart中的问题更新数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!