您好,我正在做一个学校项目,但我遇到了一个问题,任何帮助都会很棒
我有一个购物车网格视图,它有一个文本框数量,用户可以在其中更改数量值,因为某种原因它没有获得新值,只是旧值谢谢!
<div class="container">
<h1>Shopping Cart</h1>
<a href="Default.aspx">< Back to Products</a>
<br /><br />
<asp:Label ID="Label2" runat="server" ></asp:Label>
<asp:Label ID="Label3" runat="server" ></asp:Label>
<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." GridLines="None" Width="100%" CellPadding="5" ShowFooter="true" DataKeyNames="ProductID" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowDeleting="grdCart_RowDeleting" >
<HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" />
<FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" />
<AlternatingRowStyle BackColor="#F8F8F8" />
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Brand" />
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
<asp:BoundField DataField="SubTotal" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<br />
<asp:Button runat="server" ID="btnUpdateCart" Text="Update Cart" OnClick="btnUpdateCart_Click" /><br />
<asp:Button ID="btnClose2" runat="server" Text="Close" Width="50px" />
</div>
背后的代码
protected void btnUpdateCart_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gvShoppingCart.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
// We'll use a try catch block in case something other than a number is typed in
// If so, we'll just ignore it.
try
{
// Get the productId from the GridView's datakeys
int productId = Convert.ToInt32(gvShoppingCart.DataKeys[row.RowIndex].Value);
Label2.Text = productId.ToString();
// Find the quantity TextBox and retrieve the value
int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
Label3.Text = quantity.ToString();
ShoppingCart.Cart.Instance.SetItemQuantity(productId, quantity);
BindData();
}
catch (FormatException) { }
}
}
mpe2.Show();
}