熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

自創Asp.net-cookies的購物車類

2022-06-13   來源: .NET編程 
    自己寫的一個 cookies 購物車類實現購物車功能實現無需注冊就可購物
    using System;
    using SystemData;
    using SystemConfiguration;
    using SystemWeb;
    using SystemWebSecurity;
    using SystemWebUI;
    using SystemWebUIWebControls;
    using SystemWebUIWebControlsWebParts;
    using SystemWebUIHtmlControls;
    //文章來源x
    public class CookieShoppingCart
    {
    ///
    /// 加入購物車
    ///
    ///
    ///
    public static void AddToShoppingCart(int ProductID int Quantity int Box)
    {
    if (HttpContextCurrentRequestCookies[ShoppingCart] == null)
    {
    HttpCookie oCookie = new HttpCookie(ShoppingCart);
    //Set Cookie to expire in hours
    oCookieExpires = DateTimeNowAddYears();
    oCookieValue = ProductIDToString() + : + QuantityToString() + : + BoxToString();
    HttpContextCurrentResponseCookiesAdd(oCookie);
    }
    //如果cookie已經存在
    else
    {
    bool bExists = false;
    HttpCookie oCookie = (HttpCookie)HttpContextCurrentRequestCookies[ShoppingCart];
    oCookieExpires = DateTimeNowAddYears();
    string ShoppingCartStr = oCookieValueToString();
    string[] arrCookie = ShoppingCartStrSplit(new char[] { });
    //查看cookie中是否有該產品
    string newCookie = ;
    for (int i = ; i < arrCookieLength; i++)
    {
    if (arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:)) == ProductIDToString()Trim())
    {
    bExists = true;
    string OldQuantity = arrCookie[i]Trim()Substring(arrCookie[i]Trim()IndexOf(:) + );//得到數量
    OldQuantity = OldQuantityRemove(OldQuantityLastIndexOf(:));
    OldQuantity = (ConvertToInt(OldQuantity) + Quantity)ToString();
    arrCookie[i] = arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:)) + : + OldQuantity + : + BoxToString();
    //HttpContextCurrentResponseWrite(arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:)) + 已存在!數量 + OldQuantity +
    );
    //HttpContextCurrentResponseWrite(arrCookie[i] +
    );
    }
    newCookie = newCookie + + arrCookie[i];
    }
    //如果沒有該產品
    if (!bExists)
    {
    oCookieValue = oCookieValue + + ProductIDToString() + : + QuantityToString() + : + BoxToString();
    }
    else
    {
    oCookieValue = newCookieSubstring();
    }
    HttpContextCurrentResponseCookiesAdd(oCookie);
    HttpContextCurrentResponseWrite(ShoppingCart: + HttpContextCurrentRequestCookies[ShoppingCart]Value);
    }
    }


    ///
    /// 移除購物車子項
    ///
    ///
    public static void RemoveShoppingCart(int ProductID)
    {
    if (HttpContextCurrentRequestCookies[ShoppingCart] != null)
    {
    HttpCookie oCookie = (HttpCookie)HttpContextCurrentRequestCookies[ShoppingCart];
    oCookieExpires = DateTimeNowAddYears();
    //Check if Cookie already contain same item
    string ShoppingCartStr = oCookieValueToString();
    string[] arrCookie = ShoppingCartStrSplit(new char[] { });
    string[] arrCookie = new string[arrCookieLength ];
    int j = ;
    string NewStr = ;
    for (int i = ; i < arrCookieLength; i++)
    {
    if (arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:)) != ProductIDToString())
    NewStr = NewStr + + arrCookie[i];
    }
    if (NewStr == )
    HttpContextCurrentResponseCookies[ShoppingCart]Value = ;
    else
    HttpContextCurrentResponseCookies[ShoppingCart]Value = NewStrSubstring();
    }
    }
    public static void UpdateShoppingCart(int ProductID int Quantity bool box)
    {
    int Box = ;
    if (!box)
    Box = ;
    if (HttpContextCurrentRequestCookies[ShoppingCart] != null)
    {
    bool bExists = false;
    HttpCookie oCookie = (HttpCookie)HttpContextCurrentRequestCookies[ShoppingCart];
    oCookieExpires = DateTimeNowAddYears();
    string ShoppingCartStr = oCookieValueToString();
    string[] arrCookie = ShoppingCartStrSplit(new char[] { });
    //查看cookie中是否有該產品
    string newCookie = ;
    for (int i = ; i < arrCookieLength; i++)
    {
    if (arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:)) == ProductIDToString()Trim())
    arrCookie[i] = arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:)) + : + QuantityToString() + : + BoxToString();
    newCookie = newCookie + + arrCookie[i];
    }
    HttpContextCurrentResponseCookies[ShoppingCart]Value = newCookieSubstring();
    }
    }
    public static DataTable GetShoppingCart()
    {
    DataTable dt = new DataTable();
    if (HttpContextCurrentRequestCookies[ShoppingCart] != null && HttpContextCurrentRequestCookies[ShoppingCart]ValueTrim() != )
    {
    HttpCookie oCookie = (HttpCookie)HttpContextCurrentRequestCookies[ShoppingCart];
    oCookieExpires = DateTimeNowAddYears();
    string ShoppingCartStr = oCookieValueToString();
    //HttpContextCurrentResponseWrite(ShoppingCartStr);
    string[] arrCookie = ShoppingCartStrSplit(new char[] { });
    //查看cookie中是否有該產品
    string newCookie = ;
    for (int i = ; i < arrCookieLength; i++)
    {
    newCookie = newCookie + + arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:));
    }
    newCookie = newCookieSubstring();
    dt = ProductGetProductByProductIds(newCookie );
    dtColumnsAdd(Quantity);
    dtColumnsAdd(Box);
    foreach (DataRow row in dtRows)
    {
    for (int i = ; i < arrCookieLength; i++)
    {
    if (arrCookie[i]Trim()Remove(arrCookie[i]IndexOf(:)) == row[ProductId]ToString())
    {
    row[Quantity] = arrCookie[i]Substring(arrCookie[i]IndexOf(:) + );
    row[Quantity] = row[Quantity]ToString()Remove(row[Quantity]ToString()IndexOf(:));
    string Box = arrCookie[i]Substring(arrCookie[i]LastIndexOf(:) + );
    if (Box == )
    row[Box] = true;
    else
    row[Box] = false;
    }
    }
    }
    }
    else
    {
    dt = DatabaseGetDataTable(select top * from View_ProductList);
    dtColumnsAdd(Quantity);
    }
    return dt;
    }
    }


From:http://tw.wingwit.com/Article/program/net/201311/11694.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.