zoukankan      html  css  js  c++  java
  • 用Cookies和HashTable制作购物车

          /// <summary>

            /// 添加到购物车

            /// </summary>

            /// <param name="Primarykey">主键,内部以','形式分开</param>

            /// <param name="Quantity">索引</param>

            public static void AddCart(string Primarykey, int Quantity)

            {

                if (string.IsNullOrEmpty(GetCookie()))//如果cookies为空

                {

                    Hashtable ht = new Hashtable();

                    ht.Add(Primarykey, Quantity);

                    AddCookies(Json.Serialize<Hashtable>(ht));

                }

                else

                {

                    Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());

                    if (ht.Contains(Primarykey))

                    {

                        Int32 s = (Int32)ht[Primarykey] + Quantity;

                        ht.Remove(Primarykey);

                        ht.Add(Primarykey, s);//更新其数量

                    }

                    else

                    {

                        ht.Add(Primarykey, Quantity);

                    }

                    AddCookies(Json.Serialize<Hashtable>(ht));

                }

            }

            /// <summary>

            /// 修改购物车的属性

            /// </summary>

            /// <param name="Primarykey"></param>

            /// <param name="OldKey"></param>

            public static void UpdateCart(string NewKey, string OldKey)

            {

                if (string.IsNullOrEmpty(GetCookie()))//如果cookies为空

                {

                }

                else

                {

                    Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());

                    if (ht.Contains(OldKey))

                    {

                        Int32 s = (Int32)ht[OldKey];

                        ht.Remove(OldKey);

                        ht.Add(NewKey, s);//更新其数量

                        AddCookies(Json.Serialize<Hashtable>(ht));

                    }

                }

            }

            /// <summary>

            /// 更新购物车数量

            /// </summary>

            /// <param name="Key"></param>

            /// <param name="Count"></param>

            public static void UpdateCartCount(string Key, int Count)

            {

                if (string.IsNullOrEmpty(GetCookie()))//如果cookies为空

                {

                }

                else

                {

                    Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());

                    if (ht.Contains(Key))

                    {

                        ht.Remove(Key);

                        ht.Add(Key, Count);//更新其数量

                        AddCookies(Json.Serialize<Hashtable>(ht));

                    }

                }

            }

            /// <summary>

            /// 获取cookies值

            /// </summary>

            public static Hashtable GetCart()

            {

                try

                {

                    if (!string.IsNullOrEmpty(GetCookie()))

                    {

                        return Json.Deserialize<Hashtable>(GetCookie());

                    }

                    else

                    {

                        return null;

                    }

                }

                catch

                {

                    return null;

                }

                //foreach (DictionaryEntry de in ht)

                //{

                //    Console.WriteLine("Key is {0},Values is {1}", de.Key, de.Value);

                //}

            }

            /// <summary>

            /// 移除购物车

            /// </summary>

            /// <param name="Primarykey">主键 0为清理所有的购物车信息</param>

            public static void ClearCart(string Primarykey)

            {

                if (Primarykey == "0")//为0的时候清理全部购物车cookies

                {

                    HttpContext.Current.Request.Cookies["ChinahooCart"].Expires = DateTime.Now.AddHours(-1);

                    HttpContext.Current.Response.Cookies.Add(HttpContext.Current.Request.Cookies["ChinahooCart"]);

                }

                else

                {

                    if (!string.IsNullOrEmpty(GetCookie()))//如果cookies为空

                    {

                        Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());

                        if (ht.Contains(Primarykey))

                        {

                            ht.Remove(Primarykey);

                        }

                        AddCookies(Json.Serialize<Hashtable>(ht));

                    }

                }

            }

            /// <summary>

            /// 添加对象到cookies

            /// </summary>

            /// <param name="strValue"></param>

            private static void AddCookies(string strValue)

            {

                HttpCookie cookie = HttpContext.Current.Request.Cookies["ChinahooCart"];

                if (cookie == null)

                {

                    cookie = new HttpCookie("ChinahooCart");

                }

                cookie.Value = strValue;

                cookie.Expires = DateTime.Now.AddYears(1);

                HttpContext.Current.Response.AppendCookie(cookie);

            }

            /// <summary>

            /// 获取cookies

            /// </summary>

            /// <returns></returns>

            private static string GetCookie()

            {

                if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["ChinahooCart"] != null)

                    return HttpContext.Current.Request.Cookies["ChinahooCart"].Value.ToString();

                return null;

            }

  • 相关阅读:
    C复制字符串
    C语言分解数组
    perlCGI编程之测试环境
    linux下c语言 读取文件
    C++的组合(Composite)模式
    C#GDI+绘制多行文本和格式化文本
    shell中引号的应用
    perlCGI编程之Apache服务器安装配置
    求二叉树的深度
    perlCGI编程之页面参数传递
  • 原文地址:https://www.cnblogs.com/zhangkjun/p/2109444.html
Copyright © 2011-2022 走看看