如何更新购物车数量,我开发了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">×</a>--%>
<asp:LinkButton ID="RemoveLB" data-toggle="tooltip" CssClass="remove" data-original-title="Remove" runat="server" CommandName="Remove" CommandArgument='<%# Eval("ProductID")%>'>
×
</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 :(得分:0)
从你的说法来看,我认为文本框是在回发上绑定的,点击事件发生在回发后,所以如果控件在回发上被数据绑定,文本框中的更新将会丢失
编辑:更具体地说,在调用事件处理程序之前发生回发后的页面加载
Edit2:在您的评论后仔细查看您的代码,您在文本框中使用Eval而不会存储您的更新,请尝试绑定