在我的gridview中,我列出了一系列来自数据库的产品,这些产品由存储过程加载。 所以在网格视图中我列出了产品名称,价格,描述等,但后来我有一个名为quantity的文本字段,用户可以在其中输入数量,当他们按下更新时我将其作为购物车插入我的购物车中
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的页面都要检查对于我的会话购物车,看看行和会话之间的产品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 :(得分: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获取产品时,在存储过程返回的列表顶部创建一个包装购物车产品列表,在Session
中存储包装类的列表假设您的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
}
}
现在将您的网格绑定到会话中的产品列表,如下所示: private void 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,我最近在购物车上工作并粘贴代码,但逻辑将保持不变。
希望这会有所帮助