此购物车实例主要有三大功能:1.折扣方案调整 2.商品列表 3.购物车
商品列表:
购物车:
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page {
public ShopCartSet ShopCart {
get {
return (ShopCartSet)Session["ShopCartSet"];
}
set {
Session["ShopCartSet"] = value;
}
}
public List<IDiscountable> DiscountsUsing {
get {
return (List<IDiscountable>)Application["DiscountsUsing"];
}
}
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) {
this.SetDataBind();
this.SetShopCartDataBind();
this.SetDiscountDataBind();
}
}
private void SetDataBind() {
ProductBLL bllProduct = new ProductBLL();
this.gvProducts.DataSource = bllProduct.GetAllProducts();
this.gvProducts.DataBind();
}
private void SetShopCartDataBind() {
this.gvShopCart.DataSource = this.ShopCart;
this.gvShopCart.DataBind();
}
private void SetDiscountDataBind() {
if (this.DiscountsUsing.Count > 0)
this.lblDiscountsDescriptions.Text = "现在,您可以享受以下折扣方案:";
this.lstDiscountsUsing.DataSource = this.DiscountsUsing;
this.lstDiscountsUsing.DataTextField = "Description";
this.lstDiscountsUsing.DataBind();
}
protected void gvProducts_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) {
int id = int.Parse((string)e.CommandArgument);
ProductBLL bllProduct = new ProductBLL();
Product product = bllProduct.GetProductById(id);
ShopCartItem item = new ShopCartItem(product, 1);
this.ShopCart.Add(item);
this.SetShopCartDataBind();
}
protected void gvShopCart_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Footer) {
e.Row.Cells[0].ColumnSpan = 3;
e.Row.Cells[1].Visible = false;
e.Row.Cells[2].Visible = false;
Label lblTotalPrice = e.Row.Cells[3].FindControl("lblTotalPrice") as Label;
lblTotalPrice.Text = string.Format("{0:F2} 元", this.ShopCart.TotalPrice);
Label lblPrice = e.Row.Cells[4].FindControl("lblPrice") as Label;
lblPrice.Text = string.Format("{0:F2} 元", this.ShopCart.Price);
}
}
protected void gvShopCart_RowCommand(object sender, GridViewCommandEventArgs e) {
int id = int.Parse((string)e.CommandArgument);
ShopCartItem item = this.ShopCart[id];
if (e.CommandName == "AddOne")
item.Count ;
if (e.CommandName == "RemoveOne") {
if (item.Count > 1)
item.Count--;
else
this.ShopCart.Remove(item);
}
if (e.CommandName == "SetCount") {
LinkButton btnSetCount = (LinkButton)e.CommandSource;
int newCount = int.Parse(((TextBox)btnSetCount.Parent.FindControl("txtCount")).Text);
item.Count = newCount;
}
if (e.CommandName == "DeleteItem")
this.ShopCart.Remove(item);
this.SetShopCartDataBind();
}
}
资源文件列表
购物车示例/购物车示例.sln , 1602
购物车示例/购物车示例.suo , 31744
购物车示例/ShopCartExample/Default.aspx , 5549
购物车示例/ShopCartExample/Default.aspx.cs , 2870
购物车示例/ShopCartExample/Global.asax , 757
购物车示例/ShopCartExample/SetDiscounts.aspx , 660
购物车示例/ShopCartExample/SetDiscounts.aspx.cs , 1626
购物车示例/ShopCartExample/StyleSheet.css , 1316
购物车示例/ShopCartExample/Web.config , 6446
购物车示例/ShopCartExample/App_Code/BLL/ProductBLL.cs , 305
购物车示例/ShopCartExample/App_Code/BLL/Discount/CountDiscount.cs , 631
购物车示例/ShopCartExample/App_Code/BLL/Discount/IDiscountable.cs , 264
购物车示例/ShopCartExample/App_Code/BLL/Discount/ItemPriceDiscount.cs , 671
购物车示例/ShopCartExample/App_Code/BLL/Discount/NewYearDiscount.cs , 300
购物车示例/ShopCartExample/App_Code/DAL/DBHelper.cs , 2548
购物车示例/ShopCartExample/App_Code/DAL/ProductDAL.cs , 920
购物车示例/ShopCartExample/App_Code/Models/Product.cs , 626
购物车示例/ShopCartExample/App_Code/Models/ShopCartItem.cs , 1055
购物车示例/ShopCartExample/App_Code/Models/ShopCartSet.cs , 1725
购物车示例/ShopCartExample/App_Data/ShopCart.mdb , 266240
