C# - 无法在网格视图中更新数量

时间:2013-07-29 12:25:52

标签: c# asp.net gridview

我正在尝试更新网格视图中的产品数量,但它没有更新。 例如,产品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");
}

0 个答案:

没有答案