这是一个ASP.net购物车的代码程序.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class cart : System.Web.UI.Page
{
public MyDb dd = new MyDb();
public string AddProID;
protected string RefreshID;
// public string total;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//if (RefreshID == "")
//{
// RefreshID =System.DateTime.Now.Ticks.ToString();
//}
if (Request.Params["mode"] == "view") //检测是否为直接查看购物车。
{
ViewShoppingCart();
Caculator();
}
if (Request.Params["productID"] != null || Request.Params["productID"] != "")
{
this.rnd.Text = Request["item"];
AddProID = Request["productID"];
UpdateShoppingCart();
Caculator();
}
}
}
public void CreateCartTable() //创建购物车
{
DataSet ds = new DataSet();
DataTable newDT = new DataTable("CartTable");
ds.Tables.Add(newDT);
DataColumn newDC;
newDC = new DataColumn("ProductID", System.Type.GetType("System.Int32"));
ds.Tables["CartTable"].Columns.Add(newDC);
newDC = new DataColumn("Name", System.Type.GetType("System.String"));
newDC.DefaultValue = 1;
ds.Tables["CartTable"].Columns.Add(newDC);
newDC = new DataColumn("max", System.Type.GetType("System.String"));
ds.Tables["CartTable"].Columns.Add(newDC);
newDC = new DataColumn("Unit", System.Type.GetType("System.String"));
ds.Tables["CartTable"].Columns.Add(newDC);
newDC = new DataColumn("SellPrice", System.Type.GetType("System.Int32"));
ds.Tables["CartTable"].Columns.Add(newDC);
newDC = new DataColumn("IsDeleted", System.Type.GetType("System.Int32"));
newDC.DefaultValue = 0;
ds.Tables["CartTable"].Columns.Add(newDC);
Session["myCartTable"] = newDT;
ShoppingCartDlt.DataSource = ds.Tables["CartTable"].DefaultView;
ShoppingCartDlt.DataBind();
}
public void UpdateShoppingCart()
{
if (Session["myCartTable"] == null)//Session["myCartTable"]==null
{
CreateCartTable(); //调用函数CreateCartTable()新建一个DataTable
WriteShoppingCart();
}
else
{ //如果购物蓝中已有商品,则需要对购物信息表DataTable进行更新,并将其棒定到 ShoppingCartDlt
WriteShoppingCart();
}
}
public void ViewShoppingCart() //查看购物车
{
if (Session["myCartTable"] != null)
{
DataTable viewTable = new DataTable("nowCartTable");
viewTable = (DataTable)Session["myCartTable"];
ShoppingCartDlt.DataSource = viewTable.DefaultView; //购物车棒定到ShoppingCartDlt
ShoppingCartDlt.DataBind();
}