ShoppingCart中的问题更新数量 [英] Problem update quantity in ShoppingCart

查看:70
本文介绍了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");
    }

推荐答案

您实现的方式似乎为item1&项目2.

在此处查看如何实现DataList按钮单击:启用DataList行高亮并单击事件回发 [ ^ ]

使用Visual Studio DEBUGGER并查看工作流中有关什么值触发AddCart();
The way you have implemented, looks like AddCart method is getting fired for both item1 & item2.

Look here on how to implement DataList button click: Enable DataList Row Highliting and Click Event Postback[^]

Use Visual Studio DEBUGGER and see the workflow on what value is triggering the AddCart();


这篇关于ShoppingCart中的问题更新数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆