0

我正在尝试在网格视图中更新产品的数量,但它没有更新。例如,产品 A 目前在购物车中有 1 个数量,我想编辑它以增加数量(例如 3),但是点击更新后,数量仍然是 1。

有人可以向我解释我做错了什么吗?

<asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
            <asp:Label ID="lblQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="tbQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

public static bool UpdateShopCartQuantity(int intProductID, int intQuantity)
{
    bool retStatus = false;
    // get shopping data from Session
    DataTable shoppingCartDataTable = (DataTable)HttpContext.Current.Session["Cart"];

    //  Find if ProductID already exists in Shopping Cart 
    DataRow dr1 = shoppingCartDataTable.Rows.Find(intProductID);
    if (dr1 != null)
    {
        // remove item if quantity is 0
        if (intQuantity == 0)
            dr1.Delete();
        else
        {
            dr1["Quantity"] = intQuantity;
        }
        // store back shopping cart in session
        HttpContext.Current.Session["Cart"] = shoppingCartDataTable;
        retStatus = true;
    }
    return retStatus;
}    

protected void gvCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int selectedRow = e.RowIndex;
    //get ProductID
    int productID = (int)gvCart.DataKeys[selectedRow].Value;
    //get current grid row
    GridViewRow row = (GridViewRow)gvCart.Rows[selectedRow];

    TextBox tQuantity = (TextBox)row.FindControl("tbQuantity");
    int intQty = Convert.ToInt32(tQuantity.Text);
    //get shopping cart from Session
    DataTable shoppingCartDataTable = (DataTable)Session["Cart"];

    if (clsShopCart.UpdateShopCartQuantity(productID, intQty) == false)
        lblError.Text = "Update Cart fail";
    else
        lblError.Text = "Update Cart done";

    //switch to view mode
    gvCart.EditIndex = -1;
    bindGridShopCart();
    lblTotal.Text = "Total: " + clsShopCart.GetShopCartTotalPrice().ToString("C");
}
4

0 回答 0