protected void btnUpdateCart_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.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 string productId =(GridView1.DataKeys[row.RowIndex].Value).ToString(); // Find the quantity TextBox and retrieve the value int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text); ShoppingCart.Instance.SetItemQuantity(productId, quantity); } catch (FormatException) { } } } BindData(); }
<asp:GridView ID="GridView1" runat="server" DataKeyNames="pid" Width="100%" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound" ShowFooter="true" EmptyDataText="There is no object in your cart"> <RowStyle BackColor="White" /> <Columns> <asp:BoundField DataField="Description" HeaderText="Description" /> <asp:TemplateField HeaderText="Quantity"> <ItemTemplate> <asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br /> <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("pid") %>' Style="font-size: 12px;"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="UnitPrice" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}"> <HeaderStyle HorizontalAlign="Right" /> <ItemStyle HorizontalAlign="Right" /> </asp:BoundField> <asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}"> <HeaderStyle HorizontalAlign="Right" /> <ItemStyle HorizontalAlign="Right" /> </asp:BoundField> </Columns> <FooterStyle BackColor="#CCCCCC" /> <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> </asp:GridView>
if (!IsPostBack) FillGrid();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)