您现在的位置是:首页
>
你必须启用cookies 自创Asp.net-cookies的购物车类
自创A . et-cookie 的购物车类 自己写的一个 cookie 购物车类 实现购物车功能 实现无需注册就可购物 u i g Sy tem u i g Sy tem Data u i g Sy

自创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 很赞哦! (1032)
相关文章
- 怎么在淘宝上面申请开店 有谁开过淘宝 分享一下开店经验呗
- 没有淘宝店可以开快手小店吗 想开一个淘宝小店,大家帮帮忙,出出主意,看卖什么比较好
- 淘宝好零食店 我想开一家淘宝店,是卖零食的,可我不知道该卖那种零食,不知道怎样才能吸引客户,请大家出出点子,谢谢
- 想开个特色零食店 我想在网上开一家零食点求指导下
- uc自己打开软件 您好请问一下电脑版uc浏览器如何允许cookies?
- 甜品店开在什么位置好 本人想开家甜品店,求开店计划书.
- 开网店卖零食 我想开一个卖零食的网店,谁可以说一下具体的流程啊。谢谢>>
- 你必须启用cookies asp cookies用法与cookies实例教程
- 甜品店的市场需求 目前市场上最火爆的甜品店有哪些?
- 你必须启用cookies 不要离我太远(cookies演唱歌曲)详细资料大全
爱学记

微信收款码
支付宝收款码