2011-08-04 31 views
0

在我的gridview中,我列出了一堆來自數據庫的由存儲過程加載的產品。 所以在GridView我列出即產品名稱,價格,描述等,但後來我有一個名爲數量文本框,用戶可以在量輸入,當他們按更新然後我在我的購物車插入此作爲cartitemASP.NET從ShoppingCart的會話變量中填充GridView中的文本字段

Protected Sub btnUpdateCart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdateCart.Click 
    For Each row As GridViewRow In gvShoppingCart.Rows 
     If row.RowType = DataControlRowType.DataRow Then 
      ' 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 
       Dim productId = Convert.ToInt32(gvShoppingCart.DataKeys(row.RowIndex).Value) 
       ' Find the quantity TextBox and retrieve the value 
       Dim quantity = Integer.Parse(CType(row.Cells(1).FindControl("txtQuantity"), TextBox).Text) 
       ShoppingCart.Instance.SetItemQuantity(productId, quantity) 
      Catch ex As FormatException 
      End Try 
     End If 
    Next 

    BindData() 
End Sub 

因此,當我在頁面之間滑動時,例如shoppingcart.aspx和checkout.aspx,我鬆開了在這些複選框中輸入的值,所以我期待的是每次使用gridview加載頁面時都要檢查對於我的會議shoppingcart,並查看產品ID是否與行和會話匹配,如果他們確實在文本框中動態輸入該數量。我的繼承人的GridView

<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." AlternatingRowStyle-CssClass="tr_dark" HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" AllowSorting="false" Width="100%" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand"> 

    <Columns>       
      <asp:TemplateField HeaderText="Product Name" HeaderStyle-Width="130px" SortExpression="productName"> 
       <ItemTemplate> 
       <asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("description").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="Stock" HeaderStyle-Width="130px" SortExpression="address"> 
       <ItemTemplate> 
       <asp:Label ID="TradePriceField" runat="server" Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'></asp:Label> 
       </ItemTemplate>      
      </asp:TemplateField> 

    </Columns> 
    <Columns> 
    <asp:TemplateField HeaderText="Quantity" HeaderStyle-Width="10px"> 
     <ItemTemplate> 
      <asp:TextBox runat="server" Width="30" ID="txtQuantity" Text='<%# Eval("Quantity") %>'></asp:TextBox> 
     </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 

    <Columns> 
    <asp:BoundField DataField="UnitPrice" HeaderText="Actual Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" /> 
    <asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" /> 
    </Columns> 
    <Columns> 
    <asp:TemplateField HeaderText=""> 
    <ItemTemplate> 
     <asp:ImageButton ImageUrl="~/img/icons/cross.gif" width="10" height="10" alt="Cancel" runat="server" ID="btnRemove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>'/> 
    </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 

回答

0

創建一個單獨的類的產品(C#代碼):

public class CartProducts 
{ 
    public int ID { get; set; } 
    public string ProductName { get; set; } 
    public int Quantity { get; set; } 
    public decimal Price { get; set; } 
    public decimal TotalUnits { get; set; } 
    public int CategoryId { get; set; } 
    public string CategoryName { get; set; } 
    public ProductImages ProductImage { get; set; } 

    public decimal TotalItemPrice 
    { 
     get 
     { 
      return Price * TotalUnits * Quantity; 
     } 
    } 

    private string _productDesc; 
    public string ProductDesc 
    { 
     get 
     { 
      return (_productDesc.Length > 100) ? _productDesc.Substring(0, 100) + "...." : _productDesc; 
     } 
     set 
     { 

      _productDesc = value; 
     } 
    } 
} 

當您從SP得到你的產品,在上面創建的包裝車中的產品列表由您的存儲過程返回的列表中,存儲您的包裝類的列表中的會話

讓我們假設您的SP名稱是「GetallProducts」

var products = dbContext.GetallProducts(); 
List<CartProducts> products = (from prods in products select new CartProducts{ 
ID = prods.ID , 
ProductName = prods.Name, 
Price = prods.Price, 
TotalUnits = prods.TotalUnits 

}).ToList(); 

現在創建一個單獨的類將處理所有的購物車的操作,這樣的事情: 私人無效:

public class Cart 
{ 

    private List<CartProducts> _cartProducts; 
    public List<CartProducts> cartProducts 
    { 
     get 
     { 
      _cartProducts = SessionService.PullFromSession<List<CartProducts>>  (SessionKeys.Cart); 
      if (_cartProducts == null) 
      { 
       _cartProducts = new List<CartProducts>(); 
      } 
      return _cartProducts; 
     } 
     set 
     { 
      _cartProducts = value; 

      SessionService.PushToSession(SessionKeys.Cart, _cartProducts); 
     } 
    } 

    public decimal TotalPrice 
    { 
     get 
     { 
      return cartProducts.Sum(p => p.TotalItemPrice); 
     } 
    } 


    public void UpdateProduct(int quantity, int productID) 
    { 
     //Update logic here 


    } 



    public long PlaceOrder() 
    { 
     //Place order 

    } 
} 

現在隨着產品的列表,它是這樣的話綁定您的網格BindCart(){ // 車對象

 Cart c = new Cart(); 
     if (c.cartProducts.Count > 0) 
     dlistCart.DataSource = c.cartProducts; 
     dlistCart.DataBind(); 

    } 

現在每次在購物車使用這樣的更新你的物品:

var txtQuantity = e.Item.FindControl("txtQuantity") as TextBox; 
     if (e.CommandName == "update") 
     { 

      if (txtQuantity != null) 
      { 
       if (txtQuantity.Text.isInt()) 
       { 
        var count = txtQuantity.Text.toInt(); 
        int ProductID = e.CommandArgument.ToString().toInt(); 

        Cart cart = new Cart(); 
        cart.UpdateProduct(count, ProductID); 
       } 
      } 
     } 

對不起這一切的代碼是C#,因爲我不知道VB,和我最近在一個購物車,並從該粘貼代碼的工作,但邏輯將保持不變。

希望這可以幫助

+0

你能解釋我怎麼能從SP的結果集中創建產品的包裝? – StevieB

+0

我會在上面的帖子中解釋 – Parvesh