zoukankan      html  css  js  c++  java
  • 购物车的两种实现方式

     1:第一种方式:用Datatable

    BuyPro(1, Request.QueryString["id"].ToString());

     public string BuyPro(int number, string ProductCode)
            {
                 if (Session["Cart"] == null)
                {
                    this.BuildCart(number, ProductCode);
                }
                else
                {
                    DataTable cart = Session["Cart"] as DataTable;
                    if (this.Existproduct(cart, number, ProductCode))
                    {
                        this.BuildSession(cart, number, ProductCode);
                    }
                }
                return GetCartPro();
            }

    private void BuildCart(int number, string ProductCode)
            {
                DataTable cart = new DataTable();
                cart.Columns.Add("ProductCode");
                cart.Columns.Add("ProductName");
                cart.Columns.Add("ProductDes");
                cart.Columns.Add("Number");
                cart.Columns.Add("UnitPrice");
                this.BuildSession(cart, number, ProductCode);
            }
            /// <summary>
            /// 添加新产品
            /// </summary>
            /// <param name="cart"></param>
                 private void BuildSession(DataTable cart, int number, string ProductCode)
            {
                DataRow dr = cart.NewRow();
                model_pro = dal_pro.GetModel(Convert.ToInt32(ProductCode));

                dr["ProductCode"] = model_pro.id;// ViewState["ProductCode"].ToString();
                dr["ProductName"] = model_pro.ProName;
                dr["Number"] = number;
                dr["UnitPrice"] = model_pro.Mallprice;
                dr["ProductDes"] = "";// model_pro.ProductDes;
                cart.Rows.Add(dr);

                Session["Cart"] = cart;
            }


            /// <summary>
            /// 产品已经存在
            /// </summary>
            /// <param name="cart"></param>

            /// <returns></returns>

            public bool Existproduct(DataTable cart, int number, string ProductCode)
            {
                Boolean bo = false;
                foreach (DataRow dr in cart.Rows)
                {
                    if (dr["ProductCode"].ToString().Equals(ProductCode))
                    {
                        dr["Number"] = Convert.ToInt32(dr["Number"]) + number;
                        dr["Number"] = Convert.ToInt32(dr["Number"]) > 100 ? 100 : dr["Number"];
                        Session["Cart"] = cart;
                        //Response.Redirect("Cart.aspx");
                        bo = false;
                        return bo;
                    }
                    else
                    {
                        bo = true;
                    }
                }
                return bo;
            }
            protected string GetCartPro()
            {
                DataTable cart = (DataTable)Session["Cart"];

                double Total = 0;
                if (cart != null)
                {
                    foreach (DataRow dr in cart.Rows)
                    {

                        Total += Convert.ToDouble(dr["UnitPrice"].ToString()) * Convert.ToDouble(dr["Number"].ToString());

                    }
                    return "购物车已有宝贝<font style='color:red;font-weight:bold'> " + cart.Rows.Count + "</font>种,总价:¥<font style='color:red;font-weight:bold'>" + Total + "</font>";
                }
                // Maunite.Common.MessageBox.Show(this, Total.ToString());
                return "购物车目前为空,总价:0";

            }

    取出购物车信息
     private void BindGridView()
            {

                DataTable cart = Session["cart"] as DataTable;
                Mycart.DataSource = cart;
                Mycart.DataBind();
                if (Session["cart"] != null)
                {
                    Label5.Text = "您以选购的商品";
                    TotalPrice(cart);
                    BindTuijian();
                }
                else
                {
                    Label5.Text = "<font style='color:red'>您的购物车没有商品</font>";
                    TuijianDiv.Visible = false;
                    this.ltrSalary.Text = "0";
                    this.labIntegral.Text = "0";
                }

            }

    2:第二种实现方式:用Hashtable

    Hashtable hash;
                if (Session["Car"] == null)
                {
                    //如果用户没有分配购物车
                    hash = new Hashtable();         //新生成一个
                    //  hash.Add(e.CommandArgument, 1); //添加一个商品
                    // Session["Cart"] = hash;     //分配给用户
                }
                else
                {
                    //用户已经有购物车
                    hash = (Hashtable)Session["Car"];//得到购物车的hash表
                }
                if (!hash.Contains(e.CommandArgument))//购物车中已有此商品,商品数量加1
                {
                    hash.Add(e.CommandArgument, 1);//如果没有此商品,则新添加一个项

                }
                else
                {
                    int count = Convert.ToInt32(hash[e.CommandArgument].ToString());//得到该商品的数量
                    hash[e.CommandArgument] = (count + 1);//商品数量加1
                }

                Session["Car"] = hash;

    取出购物车信息

     protected void shoplist()
        {
            Hashtable Hash;
            if (Session["car"] == null)
            {
                Hash = new Hashtable();
            }
            else
            {
                Hash = (Hashtable)Session["car"];
            }
            if (Hash.Count == 0)
            {
                          Msg.Text = "您还没有购物呢?赶快购物吧!";
            }

            string[] ArrKey = new string[Hash.Count];
            int[] ArrVal = new int[Hash.Count];
            string Products = "('";
            Hash.Keys.CopyTo(ArrKey, 0);
            Hash.Values.CopyTo(ArrVal, 0);
            int k = 0;
            for (int j = 0; j < ArrKey.Length; j++)
            {
                if (k > 0) Products += "','"; k++;
                Products += ArrKey.GetValue(j).ToString();
            }
            Products += "')";
         
            SqlConnection conn = new SqlConnection(Class1.lianjie());

            conn.Open();
            string mysql = "select * from product where pid in" + Products;
            SqlCommand cmd = new SqlCommand(mysql,conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "product");
            DataTable Table1 = new DataTable();
            Table1 = ds.Tables["product"];
            Table1.Columns.Add(new DataColumn("shuliang", System.Type.GetType("System.Int32")));
         
            DataColumn[] Keys ={ Table1.Columns["pid"] };
            Table1.PrimaryKey = Keys;
            foreach (string X in Hash.Keys)
            {
                Table1.Rows.Find(X)["shuliang"] = Hash[X];
               
            }
            Table1.Columns.Add(new DataColumn("zongjia", System.Type.GetType("System.Double"), "hotprice*shuliang"));
           
         
            for (int I = 0; I < Table1.Rows.Count; I++)
            {
                TtlPrice += Convert.ToDouble(Table1.Rows[I]["zongjia"]);
              
            }
         
            Label1.Text = TtlPrice.ToString();
            Session["total"] = Label1.Text.ToString();
            MyGrid.DataSource = Table1.DefaultView;
            MyGrid.DataBind();

        }

  • 相关阅读:
    Informix IDS 11琐细管理(918测验)认证指南,第8局部:面向管理员的SQL特征(3)
    Informix IDS 11体系办理(918检验)认证指南,第8部门:面向办理员的SQL特征(1)
    DB2 9 根基(730 考试)认证指南,第 2 局部: 安然性(2)
    漫衍式 DB2 9.5 数据效能器对比
    Informix IDS 11零碎筹划(918检验)认证指南,第8部分:面向筹划员的SQL特性(1)
    Informix IDS 11系统打点(918测验)认证指南,第 7 部门: IDS复制(23)
    Informix IDS 11琐细管理(918测验)认证指南,第 7 部门: IDS复制(21)
    Informix IDS 11琐细经管(918考试)认证指南,第8局部:面向经管员的SQL特性(8)
    Informix IDS 11系统治理(918测验)认证指南,第 7 部门: IDS复制(25)
    Informix IDS 11体系管理(918检修)认证指南,第8部门:面向管理员的SQL特性(5)
  • 原文地址:https://www.cnblogs.com/zhang9418hn/p/1992961.html
Copyright © 2011-2022 走看看