Session+Hashtable实现购物车

本文主要描述了如何使用Session、Hashtable实现购物车功能,其中使用Castle.ActiveRecord来完成跟数据库的交互工作。

本程序中以下测试环境中成功运行:Vistual Studio 2005+Sql Server 2005+Castle 2.0

主要内容:
1.Hashtable简介
2.购物车实现方式
3.购物车截图

一、Hashtable简介
  在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key/value键值对均为object类型,所以Hashtable可以支持任何类型的key/value键值对。

  在哈希表中添加一个key/value键值对:HashtableObject.Add(key,value);
  在哈希表中去除某个key/value键值对:HashtableObject.Remove(key);
  从哈希表中移除所有元素:           HashtableObject.Clear();
  判断哈希表是否包含特定键key:      HashtableObject.Contains(key);

二、购物车实现方式
  首先先明确一下,购物车中需要保存哪些东西?我觉得只需保存商品ID和商品数量就可以了,为什么呢?因为商品信息是保存在数据库中的,所以只需保存了商品ID就可以从数据库中检索到商品的其它信息,如商品名、商品单价等。至于保存商品数量我想不需要解释了吧。

  根据购物车中需要保存的内容再结合Hashtable的特点,所以选用Hashtable来保存购物车信息是比较不错的选择,其中key值为商品ID,value值为商品数量,两者都为int类型。

  本购物车系统中,购物车页面为Vehicle.aspx,该页面用来处理购物车的基本操作和显示购物车,每次访问该页面时应传入两个参数:id和opt。其中id为要购买的商品ID,opt是对商品的操作,如增加、减少、删除等。当id和opt值都为0时为查看购物车。

  购物车的一些基本操作:
  a)、购买商品
    需要注意的地方:当购买商品时应判断购物车是是否已有同类商品,若有则只需要商品原有数量上递增1即可,若无只需添加一条新的数量为1的商品信息;
  b)、增加已购买商品数量
  c)、减少已购买商品数量
    需要注意的地方:当减少商品数量时,若商品数量为0则应删除此类商品;
  d)、删除已购买商品

  完成购物车的基本操之后就是显示购物车了,用以下步骤显示购物车:遍历Hashtable,每次遍历时获取商品ID和商品数量,从数据库中检索商品信息,并把必要的信息显示在页面上。下面帖一下Vehicle.aspx页面的主要代码:

None.gif protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//若id和opt值都为空则重定向
InBlock.gif
    if (Request["id"== null || Request["opt"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Response.Redirect(
"Default.aspx");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    Hashtable myHT;
InBlock.gif
InBlock.gif    
//若Session中不包含对象,则新建一个
InBlock.gif
    if (Session["Vehicle"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        myHT 
= new Hashtable();
InBlock.gif        Session[
"Vehicle"= myHT;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//从Session中获取Hashtable对象
InBlock.gif
    myHT = (Hashtable)Session["Vehicle"];
InBlock.gif
InBlock.gif    
//获取商品ID
InBlock.gif
    int intID = Convert.ToInt32(Request["id"]);
InBlock.gif    
//获取操作类型
InBlock.gif
    string strOpt = Request["opt"];
InBlock.gif
InBlock.gif    
//传入参数id=0,opt=0用于查看购物车
InBlock.gif
    if (intID != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (strOpt == "1")  //添加
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
if (myHT.Contains(intID))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myHT[intID] 
= (int)myHT[intID] + 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myHT.Add(intID, 
1);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if (strOpt == "2"//减少
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            myHT[intID] 
= (int)myHT[intID] - 1;
InBlock.gif
InBlock.gif            
if ((int)myHT[intID] == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myHT.Remove(intID);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if (strOpt == "3"//删除
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            myHT.Remove(intID);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    ShowVehicle(myHT);
ExpandedBlockEnd.gif}

None.gif
None.gif
// 查示购物车详细信息
None.gif
private   void  ShowVehicle(Hashtable myHT)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int intTempID;          //用于保存临时商品ID
InBlock.gif
    int intTempQuantity;    //用于保存临时商品数量
InBlock.gif

InBlock.gif    
int intItemCount = 0;           //购物车中商品数总计
InBlock.gif
    decimal decimalTotalMoney = 0;  //购物车中商品总金额
InBlock.gif
InBlock.gif    
//遍历Hashtable,显示购物车显示信息
InBlock.gif
    foreach (DictionaryEntry entry in myHT)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        intTempID 
= Convert.ToInt32(entry.Key.ToString());          //从哈希表中获取商品ID
InBlock.gif
        intTempQuantity = Convert.ToInt32(entry.Value);             //从哈希表中获取指定商品ID的数量
InBlock.gif

InBlock.gif        Model.Items myItems 
= Model.Items.GetItemsByID(intTempID);  //根据商品ID获得商品对象
InBlock.gif
        intItemCount += intTempQuantity;
InBlock.gif        decimalTotalMoney 
+= intTempQuantity * myItems.ItemPrice;
InBlock.gif
InBlock.gif        
//把新的商品信息添加到Table中
InBlock.gif
        TableRow tr = new TableRow();
InBlock.gif
InBlock.gif        
//显示商品名
InBlock.gif
        TableCell tc = new TableCell();
InBlock.gif        Label lbItemName 
= new Label(); ;
InBlock.gif        lbItemName.Text 
= myItems.ItemName;
InBlock.gif        tc.Controls.Add(lbItemName);
InBlock.gif        tc.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        tr.Cells.Add(tc);
InBlock.gif
InBlock.gif        
//显示商品单价
InBlock.gif
        tc = new TableCell();
InBlock.gif        Label lbItemPrice 
= new Label();
InBlock.gif        lbItemPrice.Text 
= myItems.ItemPrice.ToString();
InBlock.gif        tc.Controls.Add(lbItemPrice);
InBlock.gif        tc.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        tr.Cells.Add(tc);
InBlock.gif
InBlock.gif        
//显示商品数量
InBlock.gif
        tc = new TableCell();
InBlock.gif        ImageButton ibReduce 
= new ImageButton();
InBlock.gif        ibReduce.ImageUrl 
= "~/images/reduce.gif";
InBlock.gif        ibReduce.PostBackUrl 
= "Vehicle.aspx?id=" + intTempID + "&opt=2";
InBlock.gif        tc.Controls.Add(ibReduce);
InBlock.gif        Label lbItemQuantity 
= new Label();
InBlock.gif        lbItemQuantity.Text 
= intTempQuantity.ToString();
InBlock.gif        tc.Controls.Add(lbItemQuantity);
InBlock.gif        ImageButton ibAdd 
= new ImageButton();
InBlock.gif        ibAdd.ImageUrl 
= "~/images/add.gif";
InBlock.gif        ibAdd.PostBackUrl 
= "Vehicle.aspx?id=" + intTempID + "&opt=1";
InBlock.gif        tc.Controls.Add(ibAdd);
InBlock.gif        tc.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        tr.Cells.Add(tc);
InBlock.gif
InBlock.gif        
//显示删除操作按钮
InBlock.gif
        tc = new TableCell();
InBlock.gif        HyperLink hlDelete 
= new HyperLink();
InBlock.gif        hlDelete.Text 
= "删除";
InBlock.gif        hlDelete.NavigateUrl 
= "Vehicle.aspx?id=" + intTempID + "&opt=3";
InBlock.gif        tc.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        tc.Controls.Add(hlDelete);
InBlock.gif        tr.Cells.Add(tc);
InBlock.gif
InBlock.gif        tbVehicle.Rows.Add(tr); 
//tbVehicle为服务器Table控件,用于显示购物车信息
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    lblItemCount.Text 
= intItemCount.ToString();    //显示商品总数
InBlock.gif
    lblMoney.Text = decimalTotalMoney.ToString();   //显示商品总金额
ExpandedBlockEnd.gif
}

None.gif

 

三、购物车截图
  a)、购物首页
   购物车首页
  b)、购物车
   购物车

四、点击下载源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值