国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

自己寫的一個asp.net-cookies購物車類,作者:鄭少群,來源:http://study.pctoday.net.cn

2019-11-17 03:56:13
字體:
供稿:網(wǎng)友
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已經(jīng)存在
        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
鄭少群資料庫,計算機(jī)教程
鄭少群資料庫最新文章
ASP教程
ASP.net教程
Visual Studio教程
Ajax教程
jsp教程
java教程
Javascript教程
HTML-CSS教程
網(wǎng)絡(luò)教程
多媒體教程
系統(tǒng)教程
數(shù)據(jù)庫教程
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中是否有該產(chǎn)品
            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);//得到數(shù)量
                    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(':')) + "已存在!數(shù)量:" + 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
鄭少群資料庫,計算機(jī)教程
鄭少群資料庫最新文章
ASP教程
ASP.net教程
Visual Studio教程
Ajax教程
JSP教程
Java教程
Javascript教程
HTML-CSS教程
網(wǎng)絡(luò)教程
多媒體教程
系統(tǒng)教程
數(shù)據(jù)庫教程
J2ME教程
XML教程
            //如果沒有該產(chǎn)品
            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中是否有該產(chǎn)品
            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中是否有該產(chǎn)品
            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;

    }

}


本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/zsq86/archive/2009/12/23/5061374.aspx
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沅江市| 竹溪县| 勐海县| 古丈县| 扎囊县| 阿荣旗| 海原县| 白银市| 视频| 大化| 剑阁县| 彝良县| 株洲市| 合肥市| 湘乡市| 洞口县| 蓬安县| 商南县| 天祝| 武川县| 济宁市| 泸定县| 大渡口区| 鄂伦春自治旗| 额济纳旗| 宁安市| 左权县| 库尔勒市| 桃园县| 四川省| 汤原县| 德庆县| 安国市| 开封市| 衡水市| 正镶白旗| 绍兴市| 广灵县| 仪陇县| 日照市| 阿克苏市|