日期:2014-05-17  浏览次数:20358 次

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

------解决方案--------------------
可以用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 + "|