程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 自己寫的一個asp.net-cookies購物車類

自己寫的一個asp.net-cookies購物車類

編輯:ASP技巧

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//文章來源:http://study.pctoday.net.cn/2_asp.net.aspx
public class CookIEShoppingCart
{

    /// <summary>
    /// 加入購物車
    /// </summary>
    /// <param name="PRoductID"></param>
    /// <param name="Quantity"></param>
    public static void AddToShoppingCart(int ProductID, int Quantity, int Box)
    {
        if (HttpContext.Current.Request.CookIEs["ShoppingCart"] == null)
        {
            HttpCookie oCookie = new HttpCookIE("ShoppingCart");
            //Set CookIE to expire in 3 hours
            oCookIE.Expires = DateTime.Now.AddYears(3);
            oCookIE.Value = ProductID.ToString() + ":" + Quantity.ToString() + ":" + Box.ToString();

            HttpContext.Current.Response.Cookies.Add(oCookIE);
        }
        //如果cookIE已經存在
        else
        {
            bool bExists = false;
http://www.egacn.com/
Replica Watches: Breitling, CartIEr, Chanel, Longines, Rolex, Omega, Tag Heuer Watches On Sale
Buy Full Quality Popular Luxury Watches at Amazing Price, Your One Stop Discount Swiss Watches Store www.watchstylish.com
Exclusive Replica Rolex Watches, Tag Heuer Watches Replica, CartIEr Watches online Sale!http://www.yuget.Net
http://study.pctoday.net.cn/1_ASP.aspx
http://study.pctoday.net.cn/
鄭少群博客
A Lange & Sohne
Audemars Piguet
Audi
Baume & MercIEr
Bedat & Co
Blancpain
BMW
Breguet
Breitling
Bulgari
CartIEr
Chanel
Chopard
Christian Dior
Concord
Corum
Ebel
Girard-Perregaux
Gucci
Honda
Hublot
IWC
Jacob & Co
Jaeger-LeCoultre
Longines
Louis Vuitton
Luminor Panerai
Mercedes Benz
Michele
MIGE
Montblanc
Movado
Omega
Oris
Panerai
Patek Philippe
Philip Stein
Piaget
Porsche Design
Rado
Raymond Weil
Richard Mile
Roger Dubuis
Roger Dubuis
Rolex
Romain Jerom
Tag Heuer
Tissot
U-Boat
Ulysse Nardin
Vacheron Constantin
Zenith
A Lange & Sohne
Accutron
Angular Momentum
Audemars Piguet
Baume & MercIEr
Bedat & Co
Blancpain
Breguet
Breitling
Bulgari
CartIEr
Chanel
Chopard
Christian Dior
Concord
Corum
Girard-Perregaux
Gucci
Hublot
IWC
JACQUES LEMANS
Jaeger-LeCoultre
Longines
Maurice Lacroix
Movado
Omega
Oris
Panerai
Patek Philippe
Piaget
Porsche Design
Rado
Raymond Weil
Richard Mille
Roger Dubuis
Rolex
Seiko
Swiss Army
Tag Heuer
Ulysse-Nardin
Vacheron Constantin
Zenith
A Lange & Sohne
Audemars Piguet
Baume & MercIEr
Bedat & Co
Blancpain
Breguet
Breitling
Bulgari
CartIEr
Chanel
Chopard
Christian Dior
Concord
Corum
Ebel
Girard-Perregaux
Gucci
Hublot
IWC
Jaeger-LeCoultre
Longines
MIGE
Movado
Omega
Oris
Panerai
Patek Philippe
Piaget
Porsche Design
Rado
Raymond Weil
Rolex
Tag Heuer
Ulysse Nardin
Vacheron Constantin
Zenith
鄭少群資料庫,計算機教程
鄭少群資料庫最新文章
ASP教程
ASP.Net教程
Visual Studio教程
AJax教程
JSP教程
Java教程
Javascript教程
Html-CSS教程
網絡教程
多媒體教程
系統教程
數據庫教程
J2ME教程
XML教程
            HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookIEs["ShoppingCart"];
            oCookIE.Expires = DateTime.Now.AddYears(3);
            string ShoppingCartStr = oCookIE.Value.ToString();

            string[] arrCookIE = ShoppingCartStr.Split(new char[] { ',' });
            //查看cookIE中是否有該產品
            string newCookIE = "";
            for (int i = 0; i < arrCookIE.Length; i++)
            {
                if (arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':')) == ProductID.ToString().Trim())
                {
                    bExists = true;
                    string OldQuantity = arrCookie[i].Trim().Substring(arrCookIE[i].Trim().IndexOf(':') + 1);//得到數量
                    OldQuantity = OldQuantity.Remove(OldQuantity.LastIndexOf(":"));
                    OldQuantity = (Convert.ToInt32(OldQuantity) + Quantity).ToString();
                    arrCookie[i] = arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':')) + ":" + OldQuantity + ":" + Box.ToString();
                    //HttpContext.Current.Response.Write(arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':')) + "已存在!數量:" + OldQuantity + "<br>");
                    //HttpContext.Current.Response.Write(arrCookIE[i] + "<br>");
                }
                newCookie = newCookie + "," + arrCookIE[i];
            }

http://www.egacn.com/
Replica Watches: Breitling, CartIEr, Chanel, Longines, Rolex, Omega, Tag Heuer Watches On Sale
Buy Full Quality Popular Luxury Watches at Amazing Price, Your One Stop Discount Swiss Watches Store www.watchstylish.com
Exclusive Replica Rolex Watches, Tag Heuer Watches Replica, CartIEr Watches online Sale!http://www.yuget.Net
http://study.pctoday.net.cn/1_ASP.aspx
http://study.pctoday.net.cn/
鄭少群博客
A Lange & Sohne
Audemars Piguet
Audi
Baume & MercIEr
Bedat & Co
Blancpain
BMW
Breguet
Breitling
Bulgari
CartIEr
Chanel
Chopard
Christian Dior
Concord
Corum
Ebel
Girard-Perregaux
Gucci
Honda
Hublot
IWC
Jacob & Co
Jaeger-LeCoultre
Longines
Louis Vuitton
Luminor Panerai
Mercedes Benz
Michele
MIGE
Montblanc
Movado
Omega
Oris
Panerai
Patek Philippe
Philip Stein
Piaget
Porsche Design
Rado
Raymond Weil
Richard Mile
Roger Dubuis
Roger Dubuis
Rolex
Romain Jerom
Tag Heuer
Tissot
U-Boat
Ulysse Nardin
Vacheron Constantin
Zenith
A Lange & Sohne
Accutron
Angular Momentum
Audemars Piguet
Baume & MercIEr
Bedat & Co
Blancpain
Breguet
Breitling
Bulgari
CartIEr
Chanel
Chopard
Christian Dior
Concord
Corum
Girard-Perregaux
Gucci
Hublot
IWC
JACQUES LEMANS
Jaeger-LeCoultre
Longines
Maurice Lacroix
Movado
Omega
Oris
Panerai
Patek Philippe
Piaget
Porsche Design
Rado
Raymond Weil
Richard Mille
Roger Dubuis
Rolex
Seiko
Swiss Army
Tag Heuer
Ulysse-Nardin
Vacheron Constantin
Zenith
A Lange & Sohne
Audemars Piguet
Baume & MercIEr
Bedat & Co
Blancpain
Breguet
Breitling
Bulgari
CartIEr
Chanel
Chopard
Christian Dior
Concord
Corum
Ebel
Girard-Perregaux
Gucci
Hublot
IWC
Jaeger-LeCoultre
Longines
MIGE
Movado
Omega
Oris
Panerai
Patek Philippe
Piaget
Porsche Design
Rado
Raymond Weil
Rolex
Tag Heuer
Ulysse Nardin
Vacheron Constantin
Zenith
鄭少群資料庫,計算機教程
鄭少群資料庫最新文章
ASP教程
ASP.Net教程
Visual Studio教程
AJax教程
JSP教程
Java教程
Javascript教程
Html-CSS教程
網絡教程
多媒體教程
系統教程
數據庫教程
J2ME教程
XML教程
            //如果沒有該產品
            if (!bExists)
            {
                oCookie.Value = oCookIE.Value + "," + ProductID.ToString() + ":" + Quantity.ToString() + ":" + Box.ToString();
            }
            else
            {
                oCookie.Value = newCookIE.Substring(1);
            }
            HttpContext.Current.Response.Cookies.Add(oCookIE);
            HttpContext.Current.Response.Write("ShoppingCart:" + HttpContext.Current.Request.CookIEs["ShoppingCart"].Value);

        }
    }


    /// <summary>
    /// 移除購物車子項
    /// </summary>
    /// <param name="ProductID"></param>
    public static void RemoveShoppingCart(int ProductID)
    {
        if (HttpContext.Current.Request.CookIEs["ShoppingCart"] != null)
        {
            HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookIEs["ShoppingCart"];
            oCookIE.Expires = DateTime.Now.AddYears(3);
            //Check if CookIE already contain same item
            string ShoppingCartStr = oCookIE.Value.ToString();

            string[] arrCookIE = ShoppingCartStr.Split(new char[] { ',' });
            string[] arrCookie2 = new string[arrCookIE.Length - 1];
            int j = 0;
            string NewStr = "";
            for (int i = 0; i < arrCookIE.Length; i++)
            {
                if (arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':')) != ProductID.ToString())
                    NewStr = NewStr + "," + arrCookIE[i];
            }
            if (NewStr == "")
                HttpContext.Current.Response.CookIEs["ShoppingCart"].Value = "";
            else
                HttpContext.Current.Response.CookIEs["ShoppingCart"].Value = NewStr.Substring(1);
        }
    }


    public static void UpdateShoppingCart(int ProductID, int Quantity, bool box)
    {
        int Box = 1;
        if (!box)
            Box = 0;
        if (HttpContext.Current.Request.CookIEs["ShoppingCart"] != null)
        {
            bool bExists = false;
            HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookIEs["ShoppingCart"];
            oCookIE.Expires = DateTime.Now.AddYears(3);
            string ShoppingCartStr = oCookIE.Value.ToString();

            string[] arrCookIE = ShoppingCartStr.Split(new char[] { ',' });
            //查看cookIE中是否有該產品
            string newCookIE = "";
            for (int i = 0; i < arrCookIE.Length; i++)
            {
                if (arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':')) == ProductID.ToString().Trim())
                    arrCookie[i] = arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':')) + ":" + Quantity.ToString() + ":" + Box.ToString();
                newCookie = newCookie + "," + arrCookIE[i];
            }
            HttpContext.Current.Response.Cookies["ShoppingCart"].Value = newCookIE.Substring(1);
        }
    }


    public static DataTable GetShoppingCart()
    {
        DataTable dt = new DataTable();
        if (HttpContext.Current.Request.Cookies["ShoppingCart"] != null && HttpContext.Current.Request.CookIEs["ShoppingCart"].Value.Trim() != "")
        {
            HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookIEs["ShoppingCart"];
            oCookIE.Expires = DateTime.Now.AddYears(3);
            string ShoppingCartStr = oCookIE.Value.ToString();
            //HttpContext.Current.Response.Write(ShoppingCartStr);
            string[] arrCookIE = ShoppingCartStr.Split(new char[] { ',' });
            //查看cookIE中是否有該產品
            string newCookIE = "";
            for (int i = 0; i < arrCookIE.Length; i++)
            {
                newCookie = newCookie + "," + arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':'));
            }
            newCookie = newCookIE.Substring(1);
            dt = Product.GetProductByProductIds(newCookIE, -1);
            dt.Columns.Add("Quantity");
            dt.Columns.Add("Box");

            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < arrCookIE.Length; i++)
                {
                    if (arrCookie[i].Trim().Remove(arrCookIE[i].IndexOf(':')) == row["ProductId"].ToString())
                    {
                        row["Quantity"] = arrCookie[i].Substring(arrCookIE[i].IndexOf(":") + 1);
                        row["Quantity"] = row["Quantity"].ToString().Remove(row["Quantity"].ToString().IndexOf(":"));
                        string Box = arrCookie[i].Substring(arrCookIE[i].LastIndexOf(":") + 1);
                        if (Box == "1")
                            row["Box"] = true;
                        else
                            row["Box"] = false;
                    }
                }
            }
        }
        else
        {
            dt = Database.GetDataTable("select top 0 * from VIEw_ProductList");
            dt.Columns.Add("Quantity");
        }
        return dt;

    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved