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();
}
public void WriteShoppingCart()
{
if (Request.Params["mode"] != "view") //检查是否是直接查看购物车,如果直接查看,就不再写MYCARTTABLE
{
DataTable nowTable = new DataTable("nowCartTable");
nowTable = (DataTable)Session["myCartTable"];
int pn = nowTable.Rows.Count;
int i = 0;
bool hasone = false;
int nowProdID;
while (i < pn && !hasone)
{
nowProdID = Int32.Parse(nowTable.Rows[i][0].ToString());
if (nowProdID == Int32.Parse(AddProID)) //判断购物信息表中,是否存有当前放入商品。
if(nowProdID==Int32.Parse(AddProID))
{
hasone = true;
}
else
{
i++;
}
}
if (hasone)
{
//如果已有该商品,则 hasone=true,更改该数据行
string sesToken = RefreshID;
//if (sesToken == "")
//{
DataRow oldDR;
oldDR = nowTable.Rows[i];
oldDR["max"] = Int32.Parse(oldDR["max"].ToString()) + 1;
}
else
{
//如果没有该商品,在表中新加如一行。
DataRow newDR;
double unitp;
string strSQL = "select * from Product where ProductID=" + AddProID + " and Productrnd="+this.rnd.Text+"";
dd.cmd.CommandText = strSQL;
DataSet ds = new DataSet();
// myCommand.Fill(ds, "AddP");
dd.Adp.SelectCommand = dd.cmd;
dd.Adp.Fill(ds, "Addp");
newDR = nowTable.NewRow();
newDR[0] = AddProID;
newDR[1] = ds.Tables["Addp"].Rows[0]["Name"].ToString();
unitp = Double.Parse(ds.Tables["AddP"].Rows[0]["SellPrice"].ToString()); //会员价
newDR[2] = ds.Tables["Addp"].Rows[0]["max"].ToString();
newDR[3] = ds.Tables["Addp"].Rows[0]["Unit"].ToString();
newDR[4] = ds.Tables["Addp"].Rows[0]["SellPrice"].ToString(); //第一次读库,所以总价格和单价是一样的。
//newDR[5]="0";
nowTable.Rows.Add(newDR);
dd.conn.Close();
}
ShoppingCartDlt.DataSource = nowTable.DefaultView; //将更新后的 DataTable棒定到ShoppingCartDlt
ShoppingCartDlt.DataBind();
Session["myCartTable"] = nowTable;
//重新保存更新过的DataTable
}
}
public void Caculator()
{
if (Session["myCartTable"] != null) //购物车是否为空
{
DataTable nowTable3 = new DataTable("nowCartTable3");
nowTable3 = (DataTable)Session["myCartTable"];
if (nowTable3.Rows.Count > 0) //返回购物车中是否有货物
{
int total = 0; ;
for (h = 0; h <= this.ShoppingCartDlt.Rows.Count-1; h++)
{
total += Convert.ToInt32((Int32.Parse(nowTable3.Rows[h][4].ToString()) * Double.Parse(nowTable3.Rows[h][2].ToString())));
}
((Label)ShoppingCartDlt.FooterRow.FindControl("total")).Text = Convert.ToString(total);
OrderFrom.Money = ((Label)ShoppingCartDlt.FooterRow.FindControl("total")).Text;
}
}
}
public void Update()
{
int i;
int j;
ArrayList deleteItem = new ArrayList(10);
j = 0;
DataTable nowTable2 = new DataTable("nowCartTable2");
nowTable2 = (DataTable)Session["myCartTable"];
int total = 0;
int ProductNum = 0;
for (i = 0; i <= this.ShoppingCartDlt.Rows.Count-1; i++)
{
// _item = this.ShoppingCartDlt.Items[i];
// _item = this.ShoppingCartDlt.Items[i];
TextBox CountText = (TextBox)this.ShoppingCartDlt.Rows[i].FindControl("max");//Controls[1];//_item.FindControl("CountTb");
CheckBox ProductIDCheck = (CheckBox)this.ShoppingCartDlt.Rows[i].FindControl("chkProductID");
total +=Convert.ToInt32((Int32.Parse(nowTable2.Rows[i][4].ToString()) * Double.Parse(nowTable2.Rows[i][2].ToString())));
ProductNum += Convert.ToInt32(Int32.Parse(nowTable2.Rows[i][2].ToString()));
if (ProductIDCheck.Checked)
{
nowTable2.Rows[i][5] = 1;//添加删除标记1
j = j + 1;
}
}
((Label)ShoppingCartDlt.FooterRow.FindControl("total")).Text = Convert.ToString(total);
OrderFrom.Money = ((Label)ShoppingCartDlt.FooterRow.FindControl("total")).Text;
OrderFrom.TotolNumber=ProductNum.ToString();
string strExpr = "IsDeleted>0";
DataRow[] foundRows = nowTable2.Select(strExpr);
for (int m = 0; m < foundRows.Length; m++)
{
//Console.WriteLine(foundRows[i][0]);
foundRows[m].Delete();
}
ShoppingCartDlt.DataSource = nowTable2.DefaultView;
ShoppingCartDlt.DataBind();
Session["myCartTable"] = nowTable2;
Caculator();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("index.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
Update();
}
protected void Button5_Click(object sender, EventArgs e)
{
Update();
Response.Redirect("checkout.aspx");
}
}