zoukankan      html  css  js  c++  java
  • [网络收集]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页面的主要代码:

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

        
        Hashtable myHT;

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


        
    //从Session中获取Hashtable对象
        myHT = (Hashtable)Session["Vehicle"];

        
    //获取商品ID
        int intID = Convert.ToInt32(Request["id"]);
        
    //获取操作类型
        string strOpt = Request["opt"];

        
    //传入参数id=0,opt=0用于查看购物车
        if (intID != 0)
        
    {
            
    if (strOpt == "1")  //添加
            {
                
    if (myHT.Contains(intID))
                
    {
                    myHT[intID] 
    = (int)myHT[intID] + 1;
                }

                
    else
                
    {
                    myHT.Add(intID, 
    1);
                }

            }

            
    else if (strOpt == "2"//减少
            {
                myHT[intID] 
    = (int)myHT[intID] - 1;

                
    if ((int)myHT[intID] == 0)
                
    {
                    myHT.Remove(intID);
                }

            }

            
    else if (strOpt == "3"//删除
            {
                myHT.Remove(intID);
            }

        }


        ShowVehicle(myHT);
    }


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

        
    int intItemCount = 0;           //购物车中商品数总计
        decimal decimalTotalMoney = 0;  //购物车中商品总金额

        
    //遍历Hashtable,显示购物车显示信息
        foreach (DictionaryEntry entry in myHT)
        
    {
            intTempID 
    = Convert.ToInt32(entry.Key.ToString());          //从哈希表中获取商品ID
            intTempQuantity = Convert.ToInt32(entry.Value);             //从哈希表中获取指定商品ID的数量

            Model.Items myItems 
    = Model.Items.GetItemsByID(intTempID);  //根据商品ID获得商品对象
            intItemCount += intTempQuantity;
            decimalTotalMoney 
    += intTempQuantity * myItems.ItemPrice;

            
    //把新的商品信息添加到Table中
            TableRow tr = new TableRow();

            
    //显示商品名
            TableCell tc = new TableCell();
            Label lbItemName 
    = new Label(); ;
            lbItemName.Text 
    = myItems.ItemName;
            tc.Controls.Add(lbItemName);
            tc.HorizontalAlign 
    = HorizontalAlign.Center;
            tr.Cells.Add(tc);

            
    //显示商品单价
            tc = new TableCell();
            Label lbItemPrice 
    = new Label();
            lbItemPrice.Text 
    = myItems.ItemPrice.ToString();
            tc.Controls.Add(lbItemPrice);
            tc.HorizontalAlign 
    = HorizontalAlign.Center;
            tr.Cells.Add(tc);

            
    //显示商品数量
            tc = new TableCell();
            ImageButton ibReduce 
    = new ImageButton();
            ibReduce.ImageUrl 
    = "~/images/reduce.gif";
            ibReduce.PostBackUrl 
    = "Vehicle.aspx?id=" + intTempID + "&opt=2";
            tc.Controls.Add(ibReduce);
            Label lbItemQuantity 
    = new Label();
            lbItemQuantity.Text 
    = intTempQuantity.ToString();
            tc.Controls.Add(lbItemQuantity);
            ImageButton ibAdd 
    = new ImageButton();
            ibAdd.ImageUrl 
    = "~/images/add.gif";
            ibAdd.PostBackUrl 
    = "Vehicle.aspx?id=" + intTempID + "&opt=1";
            tc.Controls.Add(ibAdd);
            tc.HorizontalAlign 
    = HorizontalAlign.Center;
            tr.Cells.Add(tc);

            
    //显示删除操作按钮
            tc = new TableCell();
            HyperLink hlDelete 
    = new HyperLink();
            hlDelete.Text 
    = "删除";
            hlDelete.NavigateUrl 
    = "Vehicle.aspx?id=" + intTempID + "&opt=3";
            tc.HorizontalAlign 
    = HorizontalAlign.Center;
            tc.Controls.Add(hlDelete);
            tr.Cells.Add(tc);

            tbVehicle.Rows.Add(tr); 
    //tbVehicle为服务器Table控件,用于显示购物车信息
        }


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

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

    四、点击下载源码

    摘自http://www.cnblogs.com/jailu/archive/2006/09/22/511885.html


  • 相关阅读:
    生日小助手源码运行的步骤
    关于生日小助手跨平台兼容性的临时解决方案
    生日小助手V3.0——跨平台的农历生日提醒软件
    生日小助手V3.1——跨平台多语言的农历生日提醒软件
    有关生日小助手的内容,请浏览生日小助手官方网站……
    生日小助手的详细规划——本博文随时更新,持续有效
    生日小助手V2.0发布了——可以正式投入使用!
    前端开发入门的几本推荐书籍
    多想一想,JS中函数声明和函数表达式的区别
    table固定宽度大小
  • 原文地址:https://www.cnblogs.com/lushuicongsheng/p/1877236.html
Copyright © 2011-2022 走看看