您现在的位置是:首页 >

你必须启用cookies 自创Asp.net-cookies的购物车类

火烧 2022-01-04 08:38:02 1073
自创A . et-cookie 的购物车类 自己写的一个 cookie 购物车类 实现购物车功能 实现无需注册就可购物 u i g Sy tem u i g Sy tem Data u i g Sy
你必须启用cookies 自创Asp.net-cookies的购物车类

自创Asp.net-cookies的购物车类  

    自己写的一个 cookies 购物车类 实现购物车功能 实现无需注册就可购物    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;    //文章来源 x    public class CookieShoppingCart    {    ///    /// 加入购物车    ///    ///    ///    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 hours    oCookie Expires = DateTime Now AddYears( );    oCookie Value = ProductID ToString() + : + Quantity ToString() + : + Box ToString();    HttpContext Current Response Cookies Add(oCookie);    }    //如果cookie已经存在    else    {    bool bExists = false;    HttpCookie oCookie = (HttpCookie)HttpContext Current Request Cookies[ ShoppingCart ];    oCookie Expires = DateTime Now AddYears( );    string ShoppingCartStr = oCookie Value ToString();    string[] arrCookie = ShoppingCartStr Split(new char[] { });    //查看cookie中是否有该产品    string newCookie = ;    for (int i = ; 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( : ) + );//得到数量    OldQuantity = OldQuantity Remove(OldQuantity LastIndexOf( : ));    OldQuantity = (Convert ToInt (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 +     );    //HttpContext Current Response Write(arrCookie[i] +     );    }    newCookie = newCookie + + arrCookie[i];    }    //如果没有该产品    if (!bExists)    {    oCookie Value = oCookie Value + + ProductID ToString() + : + Quantity ToString() + : + Box ToString();    }    else    {    oCookie Value = newCookie Substring( );    }    HttpContext Current Response Cookies Add(oCookie);    HttpContext Current Response Write( ShoppingCart: + HttpContext Current Request Cookies[ ShoppingCart ] Value);    }    }

    ///    /// 移除购物车子项    ///    ///    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( );    //Check if Cookie already contain same item    string ShoppingCartStr = oCookie Value ToString();    string[] arrCookie = ShoppingCartStr Split(new char[] { });    string[] arrCookie = new string[arrCookie Length ];    int j = ;    string NewStr = ;    for (int i = ; 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( );    }    }    public static void UpdateShoppingCart(int ProductID int Quantity bool box)    {    int Box = ;    if (!box)    Box = ;    if (HttpContext Current Request Cookies[ ShoppingCart ] != null)    {    bool bExists = false;    HttpCookie oCookie = (HttpCookie)HttpContext Current Request Cookies[ ShoppingCart ];    oCookie Expires = DateTime Now AddYears( );    string ShoppingCartStr = oCookie Value ToString();    string[] arrCookie = ShoppingCartStr Split(new char[] { });    //查看cookie中是否有该产品    string newCookie = ;    for (int i = ; 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( );    }    }    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( );    string ShoppingCartStr = oCookie Value ToString();    //HttpContext Current Response Write(ShoppingCartStr);    string[] arrCookie = ShoppingCartStr Split(new char[] { });    //查看cookie中是否有该产品    string newCookie = ;    for (int i = ; i < arrCookie Length; i++)    {    newCookie = newCookie + + arrCookie[i] Trim() Remove(arrCookie[i] IndexOf( : ));    }    newCookie = newCookie Substring( );    dt = Product GetProductByProductIds(newCookie );    dt Columns Add( Quantity );    dt Columns Add( Box );    foreach (DataRow row in dt Rows)    {    for (int i = ; i < arrCookie Length; 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 = Database GetDataTable( select top * from View_ProductList );    dt Columns Add( Quantity );    }    return dt;    }    } lishixinzhi/Article/program/net/201311/11694  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码