我在自己做一个购物的网站,现在正在设计购物车和订单的功能.购物车的功能要有修改商品数量、删除购物车里的信息、显示购物车里的总金额、提交订单.
请问这个购物车应该如何实现??有没有相关的例子可以看??大家都来帮忙!!

解决方案 »

  1.   

    和一般的数据库一样啊,不过很多网站购物车的信息都是保存在session,开始并不保存到数据库,比如alibaba,ebay等就是这样保存在session!!!
      

  2.   

    这种例子很多的.
    可以保存到数据库,也可以保存在Session中
    你也可以看一下PetShop的购物车
      

  3.   

    网上下格Petshop或者是Duwanish看看,又可以得到你想要的,又可以学会规范的编程模式
      

  4.   

    petshop~~~~
    session保存一个购物车的实体类
      

  5.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace BookStore
    {
    /// <summary>
    /// AddToCart 的摘要说明。
    /// </summary>
    public class AddToCart : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label lbInfo;
    protected System.Web.UI.HtmlControls.HtmlForm QuickSearchFrom;
    protected System.Web.UI.WebControls.Label lbAllPrice;
    protected System.Web.UI.WebControls.Label lbAllDiscountPrice;
    protected System.Web.UI.WebControls.Label lbDiscount;
    protected System.Web.UI.WebControls.Button btToOrder;
    protected System.Web.UI.WebControls.Button btClearAll;
    protected System.Web.UI.WebControls.DataList dlCart;
    static int nBookID, nUserID;
    protected System.Web.UI.WebControls.Button btGoOn; private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    if(Object.Equals(Session["UserName"],null))
    {
    Session["ReqestedURL"] = Request.RawUrl;
    Response.Redirect("../Error.aspx");
    }
    else
    {
    nUserID = int.Parse(Session["UserID"].ToString());
    if(Object.Equals(Request["BookID"],null))
    {
    BindData();
    }
    else
    {
    nBookID = int.Parse(Request["BookID"].ToString());
    AddDataToCart();
    BindData();
    }
    }
    }
    } private void AddDataToCart()
    {
    string strSql;

    strSql = "select Quantity from ShoppingCart where BookID=" + nBookID + " and UserID=" + nUserID;
    if(IsExist(strSql))
    {
    this.lbInfo.Text = "这本书已经在购物车中了,请直接更改这本书的购买数目。";
    }
    else
    {
    strSql = "Insert into [ShoppingCart] (UserID,BookID,Quantity) Values("
    + nUserID + ","
    + nBookID + ","
    + 1 + ")";
    try
    {
    RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
    }
    catch
    {
    this.lbInfo.Text = "加入购物车失败!";
    return;
    }
    }
    }
    private void BindData()
    {
    float fAllPrice, fAllDiscountPrice;
    fAllPrice = 0;
    fAllDiscountPrice = 0;
    DataTable currentDT; RobertSoft.BookStore.OrderBook currentOrder = new RobertSoft.BookStore.OrderBook(); currentDT = currentOrder.OrderDetail(nUserID);
    DataRow currentDR;

    for(int i=0; i < currentDT.Rows.Count; i++)
    {
    currentDR = currentDT.Rows[i];
    fAllPrice += float.Parse(currentDR[4].ToString());
    fAllDiscountPrice += float.Parse(currentDR[5].ToString());
    }
    this.dlCart.DataSource = currentDT.DefaultView;
    this.dlCart.DataBind(); this.lbAllPrice.Text = fAllPrice.ToString();
    this.lbAllDiscountPrice.Text = fAllDiscountPrice.ToString();
    float fDiscount = fAllPrice - fAllDiscountPrice;
    int nDiscount = (int) (fDiscount * 10);
    fDiscount = (float)nDiscount / 10;
    this.lbDiscount.Text = fDiscount.ToString();
    } private bool IsExist(string str)
    {
    try
    {
    RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLForValue(str);
    return true;
    }
    catch
    {

    return false;
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.dlCart.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCart_ItemCommand);
    this.dlCart.EditCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCart_EditCommand);
    this.dlCart.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCart_DeleteCommand);
    this.btToOrder.Click += new System.EventHandler(this.btToOrder_Click);
    this.btClearAll.Click += new System.EventHandler(this.btClearAll_Click);
    this.btGoOn.Click += new System.EventHandler(this.btGoOn_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    private void dlCart_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
    if(e.CommandName == "BookNameClick")
    {
    string url;
    url = "../ShowBookDetail.aspx?ID=" + e.CommandArgument.ToString();
    Response.Redirect(url);
    }
    } private void dlCart_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
    int intBookID = int.Parse(e.CommandArgument.ToString());
    DeleteData(intBookID);
    BindData();

    } private void dlCart_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
    int intBookID = int.Parse(e.CommandArgument.ToString());
    string number = ((TextBox)(e.Item.Controls[7])).Text.Trim();
    int n = int.Parse(number);
    UpdateData(intBookID,n);
    BindData();

    } private void UpdateData(int intBookID, int intQuantity)
    {
    string strSql = "Update [ShoppingCart] set Quantity="
    + intQuantity + " where BookID=" + intBookID + " and UserID=" + nUserID;
    try
    {
    RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
    this.lbInfo.Text = "修改成功";
    }
    catch
    {
    this.lbInfo.Text = "更新失败!";
    return;
    }
    } private void DeleteData(int intBookID)
    {
    string strSql = "DELETE FROM [ShoppingCart] where BookID=" + intBookID + " and UserID=" + nUserID;
    try
    {
    RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
    this.lbInfo.Text = "删除成功";
    }
    catch
    {
    this.lbInfo.Text = "删除失败!";
    return;
    }
    } private void btToOrder_Click(object sender, System.EventArgs e)
    {
    if(this.dlCart.Items.Count != 0)
    {
    string strUrl;
    strUrl = "CartToOrder.aspx?UserID=" + nUserID;
    Response.Redirect(strUrl);
    }
    else
    {
    this.lbInfo.Text = "您的购物车中还没有物品!";
    this.lbInfo.ForeColor = Color.Red;
    }
    } private void btClearAll_Click(object sender, System.EventArgs e)
    {
    if(this.dlCart.Items.Count != 0)
    {
    string strSql = "DELETE FROM [ShoppingCart] where UserID=" + nUserID;
    try
    {
    RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
    BindData();
    this.lbInfo.Text = "购物车清空成功";
    }
    catch
    {
    this.lbInfo.Text = "购物车清空失败!";
    return;
    }
    }
    else
    {
    this.lbInfo.Text = "您的购物车中还没有物品!";
    this.lbInfo.ForeColor = Color.Red;
    }
    } private void btGoOn_Click(object sender, System.EventArgs e)
    {
    Response.Redirect("../Default.aspx");
    }
    }
    }
    一个简单的例子~~~~