ASP.NET 购物车 Gridview

发布于 2024-11-26 21:53:45 字数 4488 浏览 1 评论 0 原文

嘿,我正在建一家商店(无需付款),

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>

Hey I am building a shop (without payment)

i.e.

1) Select Products
2) Checkout
3) Confirmation

The selecting products screen will look like a gridview which will display

name price quantity
Text Text Input box

So i.e. lets say I have 10 products per page so i.e. 10 quantity input boxes per gridview.

And then I have 2 Buttons below my gridview i.e. Update and Checkout.

Just even worrying about Update for now.

How could that work i.e. would I have to check every input box in gridview for which a value was entered ?

I.e. is there any example how I can loop through gridview row check for a value which is greater than 0 or null for example and if there is a value then perform a function like

ShoppingCart.Instance.AddItem(5, 5)

enter image description here

My Gridview Code at the moment

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷港 2024-12-03 21:53:45

将此代码放入按钮点击事件中,以迭代 productList GridView 的每一行并调用 ShoppingCart.Instance.AddItem(productId, qty)或您想要的任何其他方法,更新产品列表及其数量:

For i As Integer = 0 To Me.productListTable.Rows.Count - 1
            Dim txtQuantity As TextBox = CType(productListTable.Rows(i).FindControl("txtQuantity"), TextBox)
            If Not txtQuantity Is Nothing Then
                Dim qty As Integer = 0
                If txtQuantity.Text.Trim() <> String.Empty Then
                    qty = Integer.Parse(txtQuantity.Text.Trim())

                    'get unique id of product - it can be SKU code or whatever that is unique for every product
                    Dim productId As String = productListTable.DataKeys(i).Value

                    'Update product list and quantities
                    ShoppingCart.Instance.AddItem(productId, 5)
                End If
            End If
        Next

如果您在 ShoppingCart.Instance.AddItem 方法中注意到,有一个我传递的产品 ID,因此我们在代码中更新正确的项目,为了检索这个唯一键,您需要在 GridView 标记中添加 DataKeyName 属性,其值等于唯一键的名称...例如:DataKeyNames="ProductId"< /强>

<asp:GridView ID="productListTable" runat="server" DataSourceID="srcProductListPerCustomer" DataKeyNames="ProductId"
        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%">

Place this code in your button click event to iterate through each row of your productList GridView and call ShoppingCart.Instance.AddItem(productId, qty) or any other method you want, to update the product list and their quantities:

For i As Integer = 0 To Me.productListTable.Rows.Count - 1
            Dim txtQuantity As TextBox = CType(productListTable.Rows(i).FindControl("txtQuantity"), TextBox)
            If Not txtQuantity Is Nothing Then
                Dim qty As Integer = 0
                If txtQuantity.Text.Trim() <> String.Empty Then
                    qty = Integer.Parse(txtQuantity.Text.Trim())

                    'get unique id of product - it can be SKU code or whatever that is unique for every product
                    Dim productId As String = productListTable.DataKeys(i).Value

                    'Update product list and quantities
                    ShoppingCart.Instance.AddItem(productId, 5)
                End If
            End If
        Next

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 add DataKeyName property in your GridView tags with value equals to the name of unique key... e.g.: DataKeyNames="ProductId"

<asp:GridView ID="productListTable" runat="server" DataSourceID="srcProductListPerCustomer" DataKeyNames="ProductId"
        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%">
深海夜未眠 2024-12-03 21:53:45

您可以在输入更新的数量时使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文