这个主要用Session跟Hashtable,前面的就不写了,
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;
using System.Data.SqlClient;
}
}
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;
using System.Data.SqlClient;
namespace shop
{
/// <summary>
/// Addtocart 的摘要说明。
/// </summary>
public class Addtocart : System.Web.UI.Page
{
SqlConnection mycon;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string prod_id=Request.QueryString["prodID"].ToString();
// mycon=DB.mycon();
// mycon.Open();
if(Session["bus"]==null)//如果是第一次购物,就生成新的hsahtable
{
System.Collections.Hashtable ht=new Hashtable();
ht.Add(prod_id,1);//将商品的ID加到hashtable中
Session["bus"]=ht;//用Session保存hasthtable
}
else//如果一开始有购物,则用以前的购物车,并加上最后买的商品
{
System.Collections.Hashtable ht=(Hashtable)Session["bus"];
if(ht[prod_id]==null)
{
ht[prod_id]=1;
}
else
{
ht[prod_id]=(int)ht[prod_id]+1;
}
Session["bus"]=ht;
}
Response.Redirect("cart.aspx?storeID="+Request.QueryString["storeID"].ToString());//跳转到购物车的页面偶这个是一个大的商城,商城里还有小店,所以的再传个商店的ID
{
/// <summary>
/// Addtocart 的摘要说明。
/// </summary>
public class Addtocart : System.Web.UI.Page
{
SqlConnection mycon;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string prod_id=Request.QueryString["prodID"].ToString();
// mycon=DB.mycon();
// mycon.Open();
if(Session["bus"]==null)//如果是第一次购物,就生成新的hsahtable
{
System.Collections.Hashtable ht=new Hashtable();
ht.Add(prod_id,1);//将商品的ID加到hashtable中
Session["bus"]=ht;//用Session保存hasthtable
}
else//如果一开始有购物,则用以前的购物车,并加上最后买的商品
{
System.Collections.Hashtable ht=(Hashtable)Session["bus"];
if(ht[prod_id]==null)
{
ht[prod_id]=1;
}
else
{
ht[prod_id]=(int)ht[prod_id]+1;
}
Session["bus"]=ht;
}
Response.Redirect("cart.aspx?storeID="+Request.QueryString["storeID"].ToString());//跳转到购物车的页面偶这个是一个大的商城,商城里还有小店,所以的再传个商店的ID
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
以上是只是个开始,正文刚开始,嘻嘻!!下面是购物车的主体部分!
#endregion
}
}
以上是只是个开始,正文刚开始,嘻嘻!!下面是购物车的主体部分!
namespace shop
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// cartmain 的摘要说明。
/// </summary>
public class cartmain : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Label mon1;
protected System.Web.UI.WebControls.Label mon;
protected System.Web.UI.WebControls.Label count;
protected System.Web.UI.WebControls.DataGrid bus;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.Label errmessage;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
SqlConnection mycon;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
mycon=DB.mycon();
mycon.Open();
//绑定推荐的商店
this.DataList1.DataSource=DB.mytable("select top 10 * from netstore where store_tj=1 order by builddate","netstore");
this.DataList1.DataBind();
CartBind();
//重新计算价格
CountMoney();
}
}
//计算节省的钱
public double reduce(double sc,double rm)
{
double sheng=sc-rm;
return sheng;
}
public string getleft(string a)
{
string b=a;
if(a.Length>8)
b=a.Substring(0,8).ToString()+"...";
return b;
}
public string getleft1(string a)
{
string b=a;
if(a.Length>6)
b=a.Substring(0,6).ToString()+"...";
return b;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// cartmain 的摘要说明。
/// </summary>
public class cartmain : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Label mon1;
protected System.Web.UI.WebControls.Label mon;
protected System.Web.UI.WebControls.Label count;
protected System.Web.UI.WebControls.DataGrid bus;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.Label errmessage;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
SqlConnection mycon;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
mycon=DB.mycon();
mycon.Open();
//绑定推荐的商店
this.DataList1.DataSource=DB.mytable("select top 10 * from netstore where store_tj=1 order by builddate","netstore");
this.DataList1.DataBind();
CartBind();
//重新计算价格
CountMoney();
}
}
//计算节省的钱
public double reduce(double sc,double rm)
{
double sheng=sc-rm;
return sheng;
}
public string getleft(string a)
{
string b=a;
if(a.Length>8)
b=a.Substring(0,8).ToString()+"...";
return b;
}
public string getleft1(string a)
{
string b=a;
if(a.Length>6)
b=a.Substring(0,6).ToString()+"...";
return b;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//从hathtable里读取ID绑定在购物车上
private void CartBind()
{
this.bus.DataSource=(Hashtable)Session["bus"];
this.bus.DataBind();
//绑定其它列
BindOther();
//重新计算价钱
CountMoney();
}
//绑定DataGrid上的其它列
private void BindOther()
{
for(int i=0; i<this.bus.Items.Count;i++)
{
mycon=DB.mycon();
mycon.Open();
SqlCommand cmd=new SqlCommand("select * from production where prod_id='"+this.bus.Items[i].Cells[1].Text+"'",mycon);
SqlDataReader sdr=cmd.ExecuteReader();
if(sdr.Read())
{
this.bus.Items[i].Cells[2].Text=sdr["prod_name"].ToString();
this.bus.Items[i].Cells[3].Text=sdr["prod_scprice"].ToString();
bus.Items[i].Cells[4].Text=sdr["prod_rmprice"].ToString();
bus.Items[i].Cells[6].Text=sdr["prod_rmprice"].ToString();
//获取购物品的数量,进行计算总价格
TextBox num=(TextBox)bus.Items[i].Cells[5].FindControl("num");
bus.Items[i].Cells[7].Text=Convert.ToString(Convert.ToDouble(bus.Items[i].Cells[6].Text)*Convert.ToDouble(num.Text));
string storeID=Request.QueryString["storeID"].ToString();
}
cmd.Dispose();
sdr.Close();
mycon.Close();
}
}
//更新购物车(当更改购物的数量或删除后的更新操作)
private void UpdateCart()
{
//遍历bus并取得里面的所有的num和ckBox的值
for(int c=0;c<bus.Items.Count;c++)
{
errmessage.Text="";
TextBox num=(TextBox)bus.Items[c].FindControl("num");
CheckBox ckBox=(CheckBox)bus.Items[c].FindControl("ckBox");
try
{
if(int.Parse(num.Text)<0)
{
errmessage.Text="您至少有一项输入错误,请检查后重新输入!";
return;
}
}
catch
{
errmessage.Text="您至少有一项输入错误,请检查后重新输入!";
return;
}
//捕捉异常
try
{
//获取选中的商品的ID号
string PID=bus.Items[c].Cells[1].Text.ToString();
int Num=int.Parse(num.Text.ToString());
//如果购买数量改变或者删除复选框被选中
if(int.Parse(bus.DataKeys[c].ToString())!=Num||ckBox.Checked==true)
{
#endregion
//从hathtable里读取ID绑定在购物车上
private void CartBind()
{
this.bus.DataSource=(Hashtable)Session["bus"];
this.bus.DataBind();
//绑定其它列
BindOther();
//重新计算价钱
CountMoney();
}
//绑定DataGrid上的其它列
private void BindOther()
{
for(int i=0; i<this.bus.Items.Count;i++)
{
mycon=DB.mycon();
mycon.Open();
SqlCommand cmd=new SqlCommand("select * from production where prod_id='"+this.bus.Items[i].Cells[1].Text+"'",mycon);
SqlDataReader sdr=cmd.ExecuteReader();
if(sdr.Read())
{
this.bus.Items[i].Cells[2].Text=sdr["prod_name"].ToString();
this.bus.Items[i].Cells[3].Text=sdr["prod_scprice"].ToString();
bus.Items[i].Cells[4].Text=sdr["prod_rmprice"].ToString();
bus.Items[i].Cells[6].Text=sdr["prod_rmprice"].ToString();
//获取购物品的数量,进行计算总价格
TextBox num=(TextBox)bus.Items[i].Cells[5].FindControl("num");
bus.Items[i].Cells[7].Text=Convert.ToString(Convert.ToDouble(bus.Items[i].Cells[6].Text)*Convert.ToDouble(num.Text));
string storeID=Request.QueryString["storeID"].ToString();
}
cmd.Dispose();
sdr.Close();
mycon.Close();
}
}
//更新购物车(当更改购物的数量或删除后的更新操作)
private void UpdateCart()
{
//遍历bus并取得里面的所有的num和ckBox的值
for(int c=0;c<bus.Items.Count;c++)
{
errmessage.Text="";
TextBox num=(TextBox)bus.Items[c].FindControl("num");
CheckBox ckBox=(CheckBox)bus.Items[c].FindControl("ckBox");
try
{
if(int.Parse(num.Text)<0)
{
errmessage.Text="您至少有一项输入错误,请检查后重新输入!";
return;
}
}
catch
{
errmessage.Text="您至少有一项输入错误,请检查后重新输入!";
return;
}
//捕捉异常
try
{
//获取选中的商品的ID号
string PID=bus.Items[c].Cells[1].Text.ToString();
int Num=int.Parse(num.Text.ToString());
//如果购买数量改变或者删除复选框被选中
if(int.Parse(bus.DataKeys[c].ToString())!=Num||ckBox.Checked==true)
{
//判断如果数量为空或0或者删除复选框选中时就进行删除操作
if(ckBox.Checked==true||Num==0)
{
RemoveItem(PID);
}
{
RemoveItem(PID);
}
else
{
Hashtable ht=(Hashtable)Session["bus"];
if(ht.Contains(PID))
{
RemoveItem(PID);
UpdateHash(PID,Num.ToString());
}
}
}
}
catch
{
errmessage.Text="您至少有一项输入错误";
}
}
}
//删除操作的代码
private void RemoveItem(string pID)
{
//从哈希表中移除所选中的要删除的物品
Hashtable ht=(Hashtable)Session["bus"];
ht.Remove(pID);
Session["bus"]=ht;
}
//更新hashtable
private void UpdateHash(string pID,string val)
{
Hashtable ht=(Hashtable)Session["bus"];
ht.Add(pID,val);
Session["bus"]=ht;
}
//计算购物的总数量,总钱数以及打折后的钱数
private void CountMoney()
{
int cou=0;
double AllPrice=0;
double money=0;
for(int n=0;n<bus.Items.Count;n++)
{
//计算数量
TextBox num1=(TextBox)bus.Items[n].Cells[5].FindControl("num");
cou+=int.Parse(num1.Text);
//计算总钱数
double price=double.Parse(bus.Items[n].Cells[3].Text);
double num=double.Parse(num1.Text);
AllPrice+=(price*num);
//打折后的钱数
double allmon=double.Parse(bus.Items[n].Cells[7].Text);
money+=allmon;
}
count.Text=cou.ToString();
mon.Text=AllPrice.ToString();
mon1.Text=money.ToString();
}
{
Hashtable ht=(Hashtable)Session["bus"];
if(ht.Contains(PID))
{
RemoveItem(PID);
UpdateHash(PID,Num.ToString());
}
}
}
}
catch
{
errmessage.Text="您至少有一项输入错误";
}
}
}
//删除操作的代码
private void RemoveItem(string pID)
{
//从哈希表中移除所选中的要删除的物品
Hashtable ht=(Hashtable)Session["bus"];
ht.Remove(pID);
Session["bus"]=ht;
}
//更新hashtable
private void UpdateHash(string pID,string val)
{
Hashtable ht=(Hashtable)Session["bus"];
ht.Add(pID,val);
Session["bus"]=ht;
}
//计算购物的总数量,总钱数以及打折后的钱数
private void CountMoney()
{
int cou=0;
double AllPrice=0;
double money=0;
for(int n=0;n<bus.Items.Count;n++)
{
//计算数量
TextBox num1=(TextBox)bus.Items[n].Cells[5].FindControl("num");
cou+=int.Parse(num1.Text);
//计算总钱数
double price=double.Parse(bus.Items[n].Cells[3].Text);
double num=double.Parse(num1.Text);
AllPrice+=(price*num);
//打折后的钱数
double allmon=double.Parse(bus.Items[n].Cells[7].Text);
money+=allmon;
}
count.Text=cou.ToString();
mon.Text=AllPrice.ToString();
mon1.Text=money.ToString();
}
private void LinkButton1_Click(object sender, System.EventArgs e)
{
//更新购物车
UpdateCart();
//进行绑定
CartBind();
}
{
//更新购物车
UpdateCart();
//进行绑定
CartBind();
}
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(this.bus.Items.Count==0)
{
this.errmessage.Text="你还没有购买另何商品!!";
return;
}
else
{
Response.Redirect("order.aspx?storeID="+Request.QueryString["storeID"]);
}//这里就是转到订单的那个页!!
}
{
if(this.bus.Items.Count==0)
{
this.errmessage.Text="你还没有购买另何商品!!";
return;
}
else
{
Response.Redirect("order.aspx?storeID="+Request.QueryString["storeID"]);
}//这里就是转到订单的那个页!!
}
}
}