ASP.NET 购物车 Gridview
嘿,我正在建一家商店(无需付款),
即
1) 选择产品 2) 结帐 3) 确认
选择产品屏幕将看起来像一个网格视图,其中将显示
名称价格数量 文本 文本输入框
因此,假设我每页有 10 个产品,即每个 gridview 有 10 个数量输入框。
然后我的 gridview 下面有 2 个按钮,即更新和结帐。
现在甚至只担心更新。
那如何工作,即我是否必须检查 gridview 中输入值的每个输入框?
即,是否有任何示例如何循环遍历 gridview 行检查是否有大于 0 或 null 的值,如果有值,则执行类似
ShoppingCart.Instance.AddItem(5, 5)
目前我的 Gridview 代码
<asp:GridView ID="productListTable" runat="server" DataSourceID="srcProductListPerCustomer" AutoGenerateColumns="False" AlternatingRowStyle-CssClass="tr_dark" HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" EmptyDataText="No records." AllowSorting="true" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Product Name" HeaderStyle-Width="130px" SortExpression="productName">
<ItemTemplate>
<asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("productName").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Pack Size" HeaderStyle-Width="70px" SortExpression="packSize">
<ItemTemplate>
<asp:Label ID="PackSizeField" runat="server" Text='<%# Eval("packSize").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Trade Price" HeaderStyle-Width="130px" SortExpression="address">
<ItemTemplate>
<asp:Label ID="TradePriceField" runat="server" Text='<%# DisplayMoney(Eval("tradePrice").ToString())%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Discount" HeaderStyle-Width="60px" SortExpression="discount">
<ItemTemplate>
<asp:Label ID="DiscountField" runat="server" Text='<%# Eval("discount").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Actual Price" HeaderStyle-Width="130px" SortExpression="actualPrice">
<ItemTemplate>
<asp:Label ID="ActualPriceField" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Stock" HeaderStyle-Width="130px" SortExpression="stock_indicator">
<ItemTemplate>
<asp:Label ID="StockField" runat="server" Text='<%# DisplayStockLevel(Eval("stock_indicator").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtQuantity" Columns="5"></asp:TextBox><br />
<asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("product_ID_key") %>' style="font-size:12px;"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="header_req" />
<AlternatingRowStyle CssClass="tr_dark" />
<PagerStyle CssClass="pagination" />
<PagerSettings PageButtonCount="3" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Mode="NumericFirstLast" />
</asp:GridView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此代码放入按钮点击事件中,以迭代
productList
GridView
的每一行并调用ShoppingCart.Instance.AddItem(productId, qty)
或您想要的任何其他方法,更新产品列表及其数量:如果您在
ShoppingCart.Instance.AddItem
方法中注意到,有一个我传递的产品 ID,因此我们在代码中更新正确的项目,为了检索这个唯一键,您需要在GridView
标记中添加DataKeyName
属性,其值等于唯一键的名称...例如:DataKeyNames="ProductId"< /强>Place this code in your button click event to iterate through each row of your
productList
GridView
and callShoppingCart.Instance.AddItem(productId, qty)
or any other method you want, to update the product list and their quantities:If you have noticed in
ShoppingCart.Instance.AddItem
method, there is a product Id I am passing so in code we update the correct item, in order to retrieve this unique key, you need to addDataKeyName
property in yourGridView
tags with value equals to the name of unique key... e.g.: DataKeyNames="ProductId"您可以在输入更新的数量时使用 AJAX 更新每一行。执行此操作的一个简单方法是将 GridView 包装在 UpdatePanel 中,然后从那里开始。
更高级的是使用 AJAX 的各种其他方法。
基本方法是循环遍历每个 GridView 行,获取每个行的文本框值,然后根据需要更新购物车。
对于存储方面,商店数据是什么意思?这是所有产品吗?或者只是购物车数据?
如果只是购物车数据,那么对于小型网站来说,将其放入会话中就可以了。对于规模较大的站点,您可能需要考虑将其存储在数据库中。
You could update each row with AJAX as you type in the updated quantity. An easy way to do this would be to wrap the GridView in an UpdatePanel, and then go from there.
More advanced would be to use various other methods of AJAX.
The basic way though would be to loop through each GridView row, get the textbox value for each, and then update the cart if needed.
For the storage side, what do you mean by the Shop Data? Is this all the products? or just the Cart data?
If it is just the Cart data then putting this in a session is fine for a small scale site. For a larger scale site you would want to look into storing this in a database.