+ -
当前位置:首页 → 问答吧 → 购物车

购物车

时间:2011-12-10

来源:互联网

使用ASP.NET写一个购物车怎么写?

作者: TQ82185   发布时间: 2011-12-10

可以用Cookie
或者Session存储
其实就是存储 产品ID 数量

C# code
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using river.Data;

namespace river
{
    public class cs_cart_cookie
    {
        public static char Sp = '|';
        public static HttpCookie getCartCookie()
        {
            //往购物车中添加商品
            HttpCookie hc = null;
            HttpRequest Request = HttpContext.Current.Request;
            if (Request.Cookies["ShoppingCart"] == null)
            {
                //如果Cookies中不存在ShoppingCart,则创建
                hc = new HttpCookie("ShoppingCart");
            }
            else
            {
                //如果Cookies中存在ShoppingCart,则取出
                hc = Request.Cookies["ShoppingCart"];

            }
            return hc;
        }

        public static int getCartItemCount()
        {
            int count = 0;
            HttpCookie hc = getCartCookie();
            List<ShoppingCart> list = new List<ShoppingCart>();
            //循环从购物车中取出物品添加到集合
            foreach (string item in hc.Values)
            {
                if (item != null)
                {
                    count += int.Parse(hc[item].Split(Sp)[1]);
                }
            }
            return count;
        }
       

        public static List<ShoppingCart> getCartList()
        {
            HttpCookie hc = getCartCookie();
            List<ShoppingCart> list = new List<ShoppingCart>();
            //循环从购物车中取出物品添加到集合
            foreach (string item in hc.Values)
            {
                if (item != null)
                {
                    try
                    {
                        string[] w = hc[item].Split(Sp);
                        ShoppingCart gwc = new ShoppingCart();
                        DataRow dr = DB.getDr("select price,title,number,picture from products where id=" + int.Parse(w[0]));
                        if (dr != null)
                        {
                            gwc.ProductID = int.Parse(w[0]);
                            gwc.Qty = int.Parse(w[1]);
                            gwc.Size = w[2];
                            gwc.Color = w[3];
                            gwc.ProductName = Convert.ToString(dr["title"]);
                            gwc.ProductNumber = Convert.ToString(dr["number"]);
                            gwc.Price = Convert.ToDecimal(dr["price"]);
                            gwc.ProductPicture = Convert.ToString(dr["picture"]);
                            list.Add(gwc);
                        }
                    }
                    catch
                    {
                    }
                }
            }
            list.Sort();

            return list;

        }

        public static void AddToCart(int productID,int qty,string Size,string color)
        {
            HttpCookie hc = getCartCookie();
            bool flag = true;//标记在购物车中是否存在本次选择的物品

            //在购物车的Cookies中查找是否存在这次要选择的物品
            foreach (string item in hc.Values)
            {
                if (item == productID.ToString())
                {
                    flag = false;
                    break;
                }
            }
            color = HttpUtility.UrlEncode(color);
            if (flag)
            {
                //如果选择的内容在购物车中没有,则创建一个新的子键
                hc.Values.Add(productID.ToString(), productID.ToString() + "|" + qty + "|" + Size + "|"+color);
            }
            else
            {
                //如果选择的内容在购物车中没,则删除原来的,添加一个新的
                int num = int.Parse(hc.Values[productID.ToString()].Split(Sp)[1]) + qty;
                hc.Values.Remove(productID.ToString());
                hc.Values.Add(productID.ToString(), productID + "|" + num.ToString() + "|" + Size + "|" + color);
            }
            hc.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(hc);
            
        }
       
        public static void UpdateCart(int productID, int qty)
        {
            HttpCookie hc = getCartCookie();
            bool flag = true;
            string Size="";
            string color="";
            foreach (string item in hc.Values)
            {
                if (item == productID.ToString())
                {
                    string[] w = hc[item].Split(Sp);
                    Size = w[2];
                    color = w[3];
                    flag = false;
                    break;
                }
            }
            if (!flag)
            {
                //如果选择的内容在购物车中没,则删除原来的,添加一个新的
              
                hc.Values.Remove(productID.ToString());
                hc.Values.Add(productID.ToString(), productID + "|" +qty+ "|" +  Size + "|" +color);
            }
            hc.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(hc);
          
        }

        public static void DelCart(int productID)
        {
            HttpCookie hc = getCartCookie();
            hc.Values.Remove(productID.ToString());
            hc.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(hc);
        }


    }

 


    public class ShoppingCart:IComparable
    {
        public ShoppingCart()
        {

        }


        int productid;
        public int ProductID
        {
            get { return productid; }
            set { productid = value; }
        }
        int qty;
        public int Qty
        {
            get { return qty; }
            set { qty = value; }
        }
        string size;
        public string Size
        {
            get { return size; }
            set { size = value; }
        }
        string color;
        public string Color
        {
            get { return color; }
            set { color = HttpUtility.UrlDecode(value); }
        }
        string productname;
        public string ProductName
        {
            get { return productname; }
            set { productname = value; }
        }
        decimal price;
        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }

        string number;
        public string ProductNumber
        {
            get { return number; }
            set { number = value; }
        }
        string picture;
        public string ProductPicture
        {
            get { return picture; }
            set { picture = value; }
        }

        public decimal SubTotal
        {
            get { return price * qty; }
        }

        public int CompareTo(object obj)
        {
            int productID = ((ShoppingCart)obj).ProductID;
            return this.ProductID.CompareTo(productID);
        }


    }


}

作者: hewansongjuan   发布时间: 2011-12-10

这里有丰富资料

作者: dalmeeme   发布时间: 2011-12-10

相关阅读 更多