购物车类
using System;
using System.Web.UI;
using System.Collections; //使用Hashtable类必须引入该命名空间
namespace EWang.BLL
{
[Serializable]
//定义商品类,保存商品的各种属性
public class CarData
{
long _pid;
int _quality;
decimal _price;
string _chicun;
string _bianhao;
public long P_ID
{
set { _pid = value; }
get { return _pid; }
}
public int Quality
{
set { _quality = value; }
get { return _quality; }
}
public decimal Price
{
set { _price = value; }
get { return _price; }
}
public string ChiCun
{
set { _chicun = value; }
get { return _chicun; }
}
public string BianHao
{
set { _bianhao = value; }
get { return _bianhao; }
}
public CarData(long pid, int qua, decimal price,string chicun,string bianhao) //构造方法,初始化商品的各个属性
{
_pid = pid;
_quality = qua;
_price = price;
_chicun = chicun;
_bianhao = bianhao;
}
}
[Serializable]
public class ShoppingCart
{
Hashtable Cart_Orders = new Hashtable();
public ICollection Orders
{
get { return Cart_Orders.Values; }
}
/// <summary>
/// 所有商品编号列表
/// </summary>
public string OrdersPidList
{
get
{
int flag = 0;
System.Text.StringBuilder strprolist = new System.Text.StringBuilder();
foreach (DictionaryEntry entry in Cart_Orders)
{
CarData order = (CarData)entry.Value;
if (flag == 0)
{
strprolist.Append(order.P_ID);
flag = 1;
}
else
{
strprolist.Append("|" + order.P_ID);
}
}
return strprolist.ToString();
}
}
public string OrdersList
{
get
{
int flag = 0;
System.Text.StringBuilder strprolist = new System.Text.StringBuilder();
foreach (DictionaryEntry entry in Cart_Orders)
{
CarData order = (CarData)entry.Value;
if (flag == 0)
{
strprolist.Append(order.P_ID + "," + order.Price + "," + order.Quality + "," + (order.Price * order.Quality)+","+order.ChiCun+","+order.BianHao);
flag = 1;
}
else
{
strprolist.Append("|" + order.P_ID + "," + order.Price + "," + order.Quality + "," + (order.Price * order.Quality) + "," + order.ChiCun+","+order.BianHao);
}
}
return strprolist.ToString();
}
}
//public Hashtable Orders
//{
// get { return Cart_Orders; }
//}
//计算总数量
public int TotalQuality
{
get
{
Int32 total = 0;
foreach (DictionaryEntry entry in Cart_Orders)
{
CarData order = (CarData)entry.Value;
total +=order.Quality;
}
return total;
}
}
//计算总价格
public decimal TotalCost
{
get
{
decimal total = 0;
foreach (DictionaryEntry entry in Cart_Orders)
{
CarData order = (CarData)entry.Value;
total += (order.Price * order.Quality);
}
return total;
}
}
//添加物件方法
public void AddItem(CarData Order)
{
CarData order = (CarData)Cart_Orders[Order.P_ID.ToString ()];
if (order != null)
order.Quality += Order.Quality;
else
Cart_Orders.Add(Order.P_ID.ToString (), Order);
}
//删除物件
public void DeleteItem(long ItemID)
{
if (Cart_Orders[ItemID.ToString()] != null)
Cart_Orders.Remove(ItemID.ToString());
}
//更新购物车数量
public void UpdateQuality(long ItemID,int quality)
{
CarData order = (CarData)Cart_Orders[ItemID.ToString()];
if (quality > 0)
{
order.Quality = quality;
Cart_Orders[ItemID.ToString()] = order;
}
}
//更新价格
public void UpdatePrice(long ItemID, decimal price)
{
CarData order = (CarData)Cart_Orders[ItemID.ToString()];
order.Price = price;
Cart_Orders[ItemID.ToString()] = order;
}
//更新购物车所有商品的价格
public ShoppingCart UpdateAllPrice(int usertype, ShoppingCart car)
{
EWang.BLL.Product_Data bll = new Product_Data();
long pid=0L;
decimal hyprice=0.0M;
foreach (DictionaryEntry entry in car.Cart_Orders)
{
CarData order = (CarData)entry.Value;
string strkey = entry.Key.ToString();
pid = order.P_ID;
switch (usertype)
{
case 1:
hyprice = bll.GetHYPrice(pid);
break;
case 2:
hyprice = bll.GetHYPrice1(pid);
break;
case 3:
hyprice = bll.GetHYPrice2(pid);
break;
}
order.Price = hyprice;
car.Cart_Orders[strkey] = order;
}
return car;
}
/// <summary>
///清空购物车
/// </summary>
public void ClearAll()
{
Cart_Orders.Clear();
}
}
}
前台页面View_ShoppingCart.aspx
<script language="javascript" type="text/javascript">
function trim(str){return str.replace(/^\s+|\s+$/,'')}
function checkisempty(obj)
{
if(trim(obj.value)=="")
{
obj.value="1";
}
}
</script>
<table width="807" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="10"><img src="images/d01_01.jpg" width="10" height="29" /></td>
<td width="97" background="images/d01_bj.jpg"><table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle" class="shoujia">您订购的商品</td>
</tr>
<tr>
<td align="center" valign="middle"><img src="images/sanjiao04.jpg" width="7" height="4" /></td>
</tr>
</table></td>
<td width="4"><img src="images/d01_02.jpg" width="4" height="29" /></td>
<td width="696" background="images/d01_03.jpg"><table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="top5">商品名称</td>
<td width="80" align="center" class="top5">编号</td>
<td width="80" align="center" class="top5">尺寸</td>
<td width="80" align="center" class="top5">单价</td>
<td width="80" align="center" class="top5">数量</td>
<td width="80" align="center" class="top5">小计</td>
<td width="80" align="center" class="top5">删除</td>
</tr>
</table></td>
</tr>
</table>
<form id="form1" runat="server" style="0px auto; padding:0px;">
<table width="807" border="0" align="center" cellpadding="0" cellspacing="0">
<asp:Repeater ID="RepeaterCar" runat="server" EnableViewState="True" >
<ItemTemplate>
<tr>
<td width="110" height="120" align="center" valign="middle">
<img src="<%#getPic(DataBinder.Eval( Container.DataItem,"P_ID" ))%>" width="90" height="100" />
</td>
<td width="252" align="center" valign="middle" class="sousuo"><%#getProductName(DataBinder.Eval(Container.DataItem, "P_ID"))%></td>
<td width="91" align="center" valign="middle" class="sousuo"><%#DataBinder.Eval( Container.DataItem,"BianHao" )%></td>
<td width="91" align="center" valign="middle" class="sousuo"><%#DataBinder.Eval( Container.DataItem,"ChiCun" )%></td>
<td width="85" align="center" valign="middle" class="sousuo"><%#DataBinder.Eval(Container.DataItem, "Price","{0:c}")%></td>
<td width="86" align="center" valign="middle"><asp:TextBox EnableViewState="true" CssClass="inputnormal" ID="TextBox1" runat="server" Text='<%#DataBinder.Eval( Container.DataItem,"Quality" )%>' onblur="checkisempty(this);"></asp:TextBox></td>
<td width="87" align="center" valign="middle" class="sousuo"><%#CalTotal(DataBinder.Eval(Container.DataItem, "Price"), DataBinder.Eval(Container.DataItem, "Quality"))%></td>
<td width="96" align="center" valign="middle" class="sousuo"> <asp:Label ID="LabPID" runat="server" Text='<%#DataBinder.Eval( Container.DataItem,"P_ID" )%>' Visible="false"></asp:Label>
<a href="View_ShoppingCart.aspx?act=del&pid=<%#DataBinder.Eval( Container.DataItem,"P_ID" )%>" class="top1">删除</a></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td height="1" colspan="8" bgcolor="#CCCCCC"></td>
</tr>
</SeparatorTemplate>
</asp:Repeater>
</table>
<table width="807" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="440" height="50" align="left" valign="middle"><a href="Default.aspx"><img src="images/d02.jpg" width="114" height="31" border="0" /></a></td>
<td align="center" valign="middle" class="top9">总计:<asp:Literal ID="LitZJ" runat="server" EnableViewState="false"></asp:Literal></td>
<td align="right" valign="middle">
<asp:ImageButton ID="btnClear" runat="server" OnClick="btnClear_Click" style="cursor:hand;" ImageUrl="images/d03.jpg" Height="22px" ImageAlign="AbsMiddle" Width="103px" />
</td>
<td align="right" valign="middle">
<asp:ImageButton ID="BtnSave" OnClick="BtnSave_Click" runat="server" Height="22px" ImageAlign="AbsMiddle"
ImageUrl="images/d04.jpg" Width="103px" style="cursor:hand;"/></td>
</tr>
<tr>
<td height="40" colspan="4" align="center" valign="middle"><a href="MakeOrder.aspx"><img src="images/d05.jpg" width="125" height="30" border="0" /></a></td>
</tr>
</table>
</form>
代码文件:View_ShoppingCart.aspx.cs文件
protected void Page_Load(object sender, EventArgs e)
{
//if (EWang.BLL.Users.isLogin() == false)
//{
// string message = "window.alert('你还没有登录,请选择后再进行本次操作!');window.location.href='Login.aspx';";
// MessageBox.ResponseScript(this, message);
// return;
//}
//else
//{
if (Request.QueryString["act"] == "del")
{
delPro();
}
else if (Request.QueryString["act"] == "add")
{
this.AddItemToCart();
}
else
{
if (!this.IsPostBack)
{
this.BindShoppingCart();
}
}
this.strtitle = "我的购物车" + "-" + WebBasicInfo.sitename;
this.strkeywords = WebBasicInfo.keyword;
this.webSiteDesc = WebBasicInfo.webintro;
// Freeze(string.Format(@"{0}.htm", Request.Url.ToString()));
//}
}
private void AddItemToCart()
{
string url="";
if (Request.UrlReferrer != null)
{
url = Request.UrlReferrer.ToString();
}
string pid = Request["proid"].ToString();
string quality = Request["quality"].ToString();
string price=Request["price"].ToString ();
string ishavesize = Request["ishavesize"].ToString();
string bianhao = Request["hidbianhao"].ToString();
if (bianhao.Trim() == "")
{
bianhao = "无";
}
string chicun = "无";
if (ishavesize == "1")
{
chicun = Request["selSize"].ToString().Trim ();
if (chicun == "")
{
MessageBox.ResponseScript("alert('保存成功!');location.href='"+url+"';");
}
}
EWang.BLL.Product_Data bll = new EWang.BLL.Product_Data();
int num = bll.GetPNum(long.Parse(pid));
int qua = int.Parse(quality);
if ((num <= 0 || num - qua <= 0)&&url!="")
{
if (url.IndexOf("&h=u") > 0)
{
}
else
{
url = url + "&h=u";
}
Response.Redirect(url);
}
EWang.BLL.CarData order = new EWang.BLL.CarData(long.Parse(pid), Int32.Parse(quality), decimal.Parse(price), chicun, bianhao);
EWang.BLL.ShoppingCart cart = (EWang.BLL.ShoppingCart)Session["MyShoppingCart"]; //创建实例
if (cart != null)
{
cart.AddItem(order);
Session["MyShoppingCart"] = cart;
Response .Redirect("View_ShoppingCart.aspx");
}
}
private void delPro()
{
if (string.IsNullOrEmpty(Request.QueryString["pid"]))
{
Server.Transfer("View_ShoppingCart.aspx");
}
else
{
string strpid = Request.QueryString["pid"].ToString().Trim();
EWang.BLL.ShoppingCart cart = (EWang.BLL.ShoppingCart)(Session["MyShoppingCart"]);
cart.DeleteItem(long.Parse(strpid));
Session["MyShoppingCart"] = cart;
Response.Redirect("View_ShoppingCart.aspx");
}
}
private void BindShoppingCart()
{
EWang.BLL.ShoppingCart cart = (EWang.BLL.ShoppingCart)(Session["MyShoppingCart"]);
if (cart != null)
{
this.RepeaterCar.DataSource = cart.Orders;
this.RepeaterCar.DataBind();
// this.LitXJ.Text = string.Format("{0:c}", cart.TotalCost);
this.LitZJ.Text = string.Format("{0:c}", cart.TotalCost);
}
}
public string getPic(object obj)
{
EWang.BLL.Product_Data bll = new EWang.BLL.Product_Data();
return EWang.Common.FormatData.getImgSrc(bll.GetPic(long.Parse(obj.ToString ())));
}
public string getProductName(object obj)
{
EWang.BLL.Product_Data bll = new EWang.BLL.Product_Data();
return bll.GetProductName(long.Parse(obj.ToString ()));
}
public string CalTotal(object price, object quality)
{
decimal total=decimal.Parse(price.ToString ()) * Int32.Parse(quality.ToString ());
return string.Format("{0:c}",total);
}
protected void BtnSave_Click(object sender, EventArgs e)
{
Label lblPID = new Label();
TextBox txtQua=new TextBox ();
EWang.BLL.ShoppingCart cart = (EWang.BLL.ShoppingCart)(Session["MyShoppingCart"]);
foreach (RepeaterItem li in this.RepeaterCar.Items)
{
lblPID = (Label)(li.FindControl("LabPID"));
txtQua = (TextBox)(li.FindControl("TextBox1"));
cart.UpdateQuality(long .Parse(lblPID.Text.Trim ()),int.Parse(txtQua.Text.Trim ()));
}
Session["MyShoppingCart"] = cart;
Response.Redirect("View_ShoppingCart.aspx");
}
protected void btnClear_Click(object sender, EventArgs e)
{
EWang.BLL.ShoppingCart cart = (EWang.BLL.ShoppingCart)(Session["MyShoppingCart"]);
cart.ClearAll();
Session["MyShoppingCart"] = cart;
Response.Redirect("View_ShoppingCart.aspx");
}