2011-07-28 104 views
1

嘿我建立一個商店(不付款)ASP.NET購物車的GridView

1)選擇產品 2)結帳 3)確認

的選擇產品屏幕看起來like gridview which will display

name price quantity quantity Text Text Input box

即讓我們說我每頁有10個產品,即每個gridview有10個數量的輸入框。

然後我有我的gridview下面的2個按鈕,即更新和簽出。

現在甚至擔心更新。

這怎麼可能,即我將不得不檢查gridview中的每一個輸入框的輸入值?

I.e.有沒有任何例子,我可以如何循環通過gridview行檢查值大於0或null例如,如果有值,然後執行一個函數,如

ShoppingCart.Instance.AddItem(5,)

enter image description here

我的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> 

回答

1

將此代碼放在您的按鈕單擊事件通過您的productListGridView的每一行進行迭代,並調用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 =「產品」

<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%"> 
+0

酷.................... –

0

如您在更新的數量類型你可以更新AJAX每一行。一個簡單的方法就是將GridView包裝到UpdatePanel中,然後從那裏開始。

更高級的是使用各種其他AJAX方法。

雖然基本的方法是遍歷每個GridView行,獲取每個GridView的值,然後根據需要更新購物車。

對於存儲方面來說,Shop Data的含義是什麼?這是所有的產品?或只是購物車數據?

如果只是購物車數據,那麼把它放在一個會話中對於小規模網站來說可以。對於一個更大規模的網站,你會想把它存儲在數據庫中。

+0

嗨,我的意思是存儲所述車數據耶即用戶已選擇的項目。我如何將它存儲在一個會話中,即如何在會話中存儲Order和Orderlines? – StevieB

+0

你可以在會話中存儲任何東西。 '會話(「YourUniqueSessionName」)=購物車'購物車'可能是一個數據集和對象等... –

+0

我試着用谷歌搜索這個,但沒有找到存儲購物車會話中的任何好例子。即如何將單個訂單行添加到購物車會話中,以及何時檢索數據以循環查看數據 – StevieB