在线购物车不刷新

时间:2015-09-03 10:31:16

标签: vb.net

基本上每当我向在线购物篮添加商品时,它应该刷新并自己显示购物篮中的商品数量,但是由于某些原因购物车本身并不刷新并且显示正确的数量,除非手动刷新页面。任何一种解决方案都是受欢迎的。请查看网站http://grocery.egrocer.ae

/// <summary>
///     Update the quantity of item(s) in the shopping cart and then display the contents 
///     of the cart.
///     <param name="sender">The source of the event.</param>
///     <param name="e">An EventArgs that contains the event data.</param>
/// </summary>
public void UpdateButton_Click(Object sender, EventArgs e) 
{
    bool cartHasItems = false;       //Set if the cart contains items

    Cart shoppingCart = ShoppingCart(false);
    if (!shoppingCart.IsEmpty)
    {
        if (!Page.IsValid)
        {
            return;
        }

        //
        // Validators have run so we can assume valid input
        //

        //
        // reset the quanities for all the items in cart
        //
        DataRowCollection orderRows = shoppingCart.OrderItems.Rows;
        int i = 0;
        foreach (DataGridItem item in CartItemsDataGrid.Items)
        {
            //update the quantity
            orderRows[i][1] = Int32.Parse(((TextBox)item.FindControl("QuantityTextBox")).Text);
            ++i;
        }

        shoppingCart.UpdateItems();

        cartHasItems = !shoppingCart.IsEmpty;
        if (cartHasItems) 
        {
            //Bind the DataGrid to the items
            CartItemsDataGrid.DataSource = (ICollection)shoppingCart.OrderItems.DefaultView;
            CartItemsDataGrid.DataBind();
        }
    }

    //
    // Set visibility of displayed items to correspond to
    //   whether or not we have items in the cart.
    //
    ShoppingCartPanel.Visible = cartHasItems;
    CartItemsDataGrid.Visible = cartHasItems;
    CheckOutHyperLink.Visible= cartHasItems;
    EmptyCartLabel.Visible = !cartHasItems;
}

1 个答案:

答案 0 :(得分:0)

尝试在DataGridView.DataBind之后向代码添加刷新。

/// <summary>
///     Update the quantity of item(s) in the shopping cart and then display the contents 
///     of the cart.
///     <param name="sender">The source of the event.</param>
///     <param name="e">An EventArgs that contains the event data.</param>
/// </summary>
public void UpdateButton_Click(Object sender, EventArgs e)
{
    bool cartHasItems = false;
//Set if the cart contains items
    Cart shoppingCart__1 = ShoppingCart(false);
    if (!shoppingCart__1.IsEmpty) {
        if (!Page.IsValid) {
            return;
        }

        //
        // Validators have run so we can assume valid input
        //

        //
        // reset the quanities for all the items in cart
        //
        DataRowCollection orderRows = shoppingCart__1.OrderItems.Rows;
        int i = 0;
        foreach (DataGridItem item in CartItemsDataGrid.Items) {
            //update the quantity
            orderRows(i)(1) = Int32.Parse(((TextBox)item.FindControl("QuantityTextBox")).Text);
            i += 1;
        }

        shoppingCart__1.UpdateItems();

        cartHasItems = !shoppingCart__1.IsEmpty;
        if (cartHasItems) {
            //Bind the DataGrid to the items
            CartItemsDataGrid.DataSource = (ICollection)shoppingCart__1.OrderItems.DefaultView;
            CartItemsDataGrid.DataBind();
            CartItemsDataGrid.Refresh();
        }
    }

//
// Set visibility of displayed items to correspond to
//   whether or not we have items in the cart.
//
    ShoppingCartPanel.Visible = cartHasItems;
    CartItemsDataGrid.Visible = cartHasItems;
    CheckOutHyperLink.Visible = cartHasItems;
    EmptyCartLabel.Visible = !cartHasItems;
}