2015-12-29 106 views
-2

如何更新購物車的數量,我開發cartview頁面和什麼時候更新所有項目,它從文本框中取出舊的數量(綁定)的值不是新的,我寫在文本框中。如何更新購物車數量?

購物車代碼

public void SetItemQuantity(int productId, int quantity) 
     { 
      // If we are setting the quantity to 0, remove the item entirely 
      if (quantity == 0) 
      { 
       RemoveItem(productId); 
       return; 
      } 

      // Find the item and update the quantity 
      CartItem updatedItem = new CartItem(productId); 

      foreach (CartItem item in Items) 
      { 
       if (item.Equals(updatedItem)) 
       { 
        item.Quantity = quantity; 
        return; 
       } 
      } 
     } 

滿購物車

<asp:ListView ID="FullCartListView" runat="server" DataKeyNames="ProductID" OnDataBound="FullCartListView_DataBound" OnItemCommand="FullCartListView_ItemCommand"> 
 
     <EmptyDataTemplate> 
 
       <table class="table-bordered table table-cart"> 
 
     <thead> 
 
      <tr> 
 
     <th> 
 
      No items in shopping cart . 
 
     </th> 
 
      </tr> 
 
      
 
     </thead> 
 
           
 
          </table> 
 
     </EmptyDataTemplate> 
 
     <ItemTemplate> 
 
      <tbody> 
 
      <tr> 
 
      <td class="cart-product"> 
 
       <%--<a href="#" data-toggle="tooltip" title="" class="remove" data-original-title="Remove">&times;</a>--%> 
 
       <asp:LinkButton ID="RemoveLB" data-toggle="tooltip" CssClass="remove" data-original-title="Remove" runat="server" CommandName="Remove" CommandArgument='<%# Eval("ProductID")%>'> 
 
            &times; 
 
           </asp:LinkButton> 
 
       <a href="ProductDetails.aspx?ProductID=<%# Eval("ProductID") %>" title="" class="product-cart"> 
 
       <img src="<%=ConfigurationManager.AppSettings["ProductsHTMLPath"].ToString() %><%# Eval("PictureName") %>" width="72" height="100" alt=""> 
 
       <h3 class="product-title"><%# Eval("Name") %></h3> 
 
       </a> 
 
      </td> 
 

 
      <%-- <td> 
 
       <a href="#" data-toggle="tooltip" title="" class="edit-link" data-original-title="Edit"> 
 
       <i class="fa fa-edit"></i> 
 
       </a> 
 
      </td>--%> 
 

 
      <td><span class="amount"><%# Eval("Price") %> </span></td> 
 
      <td><asp:TextBox ID="QuantityTextBox" CssClass="qty" runat="server" Text='<%# Eval("quantity") %>' /> 
 
       <asp:Label runat="server" ID="ProductIDLabel" Text='<%# Eval("ProductID") %>' Visible="false" /> 
 
       <%-- <br /><br /><asp:Button ID="UpdateCartButton" runat="server" CssClass="btn btn-dark btn-outline" CommandName="Update" CommandArgument="<%# Eval("ProductID") %>" Text="Update"></asp:Button>--%> 
 
      </td> 
 
      <td><span class="amount"> 
 
       <asp:Label runat="server" ID="SubTotalPriceLabel" Text='<%# (decimal)Eval("quantity") * (decimal)Eval("Price") %>' /> 
 

 
       </span></td> 
 
      </tr> 
 
     </tbody> 
 
     </ItemTemplate> 
 
     <LayoutTemplate> 
 
      <table id="itemPlaceholderContainer" class="table-bordered table table-cart"> 
 
     <thead> 
 
      <tr> 
 
      <th>Product Name</th> 
 
      <%--<th style="width:10%"></th>--%> 
 
      <th style="width:10%">Unit</th> 
 
      <th style="width:10%">QTY</th> 
 
      <th style="width:10%">Subtotal</th> 
 
      </tr> 
 
      <tr id="itemPlaceholder" runat="server"> 
 
         </tr> 
 
     </thead> 
 
       <tfoot> 
 
      <tr> 
 
      <td colspan="5"> 
 
       <div class="text-right"> 
 
       <%--<a href="#" role="button" class="btn btn-dark btn-outline">Empty cart</a>--%> 
 
        <asp:Button ID="EmptyCartButton" runat="server" CssClass="btn btn-dark btn-outline" Text="Empty cart" OnClick="EmptyCartButton_Click"></asp:Button> 
 
       <asp:Button ID="UpdateCartButton" runat="server" CssClass="btn btn-primary" Text="Update cart" OnClick="UpdateCartButton_Click"></asp:Button> 
 
       <%--<button type="button" class="btn btn-primary">Update cart</button>--%> 
 
       </div> 
 
      </td> 
 
      </tr> 
 
     </tfoot> 
 
       </table> 
 
     </LayoutTemplate> 
 
    </asp:ListView>

代碼背後更新量的

protected void UpdateCartButton_Click(object sender, EventArgs e) 
 
     { 
 
      for (int i = 0; i < FullCartListView.Items.Count; i++) 
 
      { 
 
       
 
       TextBox quantity = (TextBox)FullCartListView.Items[i].FindControl("QuantityTextBox"); 
 
       Label productid = (Label)FullCartListView.Items[i].FindControl("ProductIDLabel"); 
 
       ShoppingCart.ShoppingCart.Instance.SetItemQuantity(Convert.ToInt32(productid.Text), Convert.ToInt32(quantity.Text)); 
 

 
      } 
 

 
      
 
      BindData(); 
 

 
     } 
 
                   
 
    protected void BindData() 
 
     { 
 
      FullCartListView.DataSource = ShoppingCart.ShoppingCart.Instance.Items; 
 
      FullCartListView.DataBind(); 
 
     }

回答

0

從你說的話,我想在文本框中被綁定在回傳,點擊回發後發生的事件,因此,如果控件在回發是數據綁定,在文本框中的更新將會丟失

編輯:更具體地說回傳後的頁面加載發生事件處理程序調用之前

EDIT2:經過你的代碼看清楚喲你的評論,你使用Eval的文本框將不會存儲您的更新,嘗試綁定而不是

+0

我忘記添加此行。 謝謝。 protected void Page_Load(object sender,EventArgs e) if(!Page.IsPostBack) {BindData(); } } – ahmed

+0

嘗試#bind而不是#eval在文本框中,eval只能讀取數據,bind是雙向讀寫 –

相關問題