《cookies购物车类(6页).doc》由会员分享,可在线阅读,更多相关《cookies购物车类(6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-cookies购物车类-第 6 页public static DataTable GetShoppingCart()DataTable dt = new DataTable();if (HttpContext.Current.Request.CookiesShoppingCart != null & HttpContext.Current.Request.CookiesShoppingCart.Value.Trim() != )HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookiesShoppingCart;oC
2、ookie.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 + , +
3、 arrCookiei.Trim().Remove(arrCookiei.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 (arrCookiei.Trim().Remove(arrCookiei.IndexOf(:)
4、 = rowProductId.ToString()rowQuantity = arrCookiei.Substring(arrCookiei.IndexOf(:) + 1);rowQuantity = rowQuantity.ToString().Remove(rowQuantity.ToString().IndexOf(:);string Box = arrCookiei.Substring(arrCookiei.LastIndexOf(:) + 1);if (Box = 1)rowBox = true;elserowBox = false;elsedt = Database.GetDat
5、aTable(select top 0 * from View_ProductList);dt.Columns.Add(Quantity);return dt;/如果没有该产品if (!bExists)oCookie.Value = oCookie.Value + , + ProductID.ToString() + : + Quantity.ToString() + : + Box.ToString();elseoCookie.Value = newCookie.Substring(1);HttpContext.Current.Response.Cookies.Add(oCookie);Ht
6、tpContext.Current.Response.Write(ShoppingCart: + HttpContext.Current.Request.CookiesShoppingCart.Value);/ / 移除购物车子项/ / public static void RemoveShoppingCart(int ProductID)if (HttpContext.Current.Request.CookiesShoppingCart != null)HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookiesS
7、hoppingCart;oCookie.Expires = DateTime.Now.AddYears(3);/Check if Cookie already contain same itemstring ShoppingCartStr = oCookie.Value.ToString();string arrCookie = ShoppingCartStr.Split(new char , );string arrCookie2 = new stringarrCookie.Length - 1;int j = 0;string NewStr = ;for (int i = 0; i arr
8、Cookie.Length; i+)if (arrCookiei.Trim().Remove(arrCookiei.IndexOf(:) != ProductID.ToString()NewStr = NewStr + , + arrCookiei;if (NewStr = )HttpContext.Current.Response.CookiesShoppingCart.Value = ;elseHttpContext.Current.Response.CookiesShoppingCart.Value = NewStr.Substring(1);public static void Upd
9、ateShoppingCart(int ProductID, int Quantity, bool box)int Box = 1;if (!box)Box = 0;if (HttpContext.Current.Request.CookiesShoppingCart != null)bool bExists = false;HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookiesShoppingCart;oCookie.Expires = DateTime.Now.AddYears(3);string Shopp
10、ingCartStr = oCookie.Value.ToString();string arrCookie = ShoppingCartStr.Split(new char , );/查看cookie中是否有该产品string newCookie = ;for (int i = 0; i arrCookie.Length; i+)if (arrCookiei.Trim().Remove(arrCookiei.IndexOf(:) = ProductID.ToString().Trim()arrCookiei = arrCookiei.Trim().Remove(arrCookiei.Inde
11、xOf(:) + : + Quantity.ToString() + : + Box.ToString();newCookie = newCookie + , + arrCookiei;HttpContext.Current.Response.CookiesShoppingCart.Value = newCookie.Substring(1);public static DataTable GetShoppingCart()DataTable dt = new DataTable();if (HttpContext.Current.Request.CookiesShoppingCart !=
12、null & HttpContext.Current.Request.CookiesShoppingCart.Value.Trim() != )HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.CookiesShoppingCart;oCookie.Expires = DateTime.Now.AddYears(3);string ShoppingCartStr = oCookie.Value.ToString();/HttpContext.Current.Response.Write(ShoppingCartStr);s
13、tring arrCookie = ShoppingCartStr.Split(new char , );/查看cookie中是否有该产品string newCookie = ;for (int i = 0; i arrCookie.Length; i+)newCookie = newCookie + , + arrCookiei.Trim().Remove(arrCookiei.IndexOf(:);newCookie = newCookie.Substring(1);dt = Product.GetProductByProductIds(newCookie, -1);dt.Columns.
14、Add(Quantity);dt.Columns.Add(Box);foreach (DataRow row in dt.Rows)for (int i = 0; i arrCookie.Length; i+)if (arrCookiei.Trim().Remove(arrCookiei.IndexOf(:) = rowProductId.ToString()rowQuantity = arrCookiei.Substring(arrCookiei.IndexOf(:) + 1);rowQuantity = rowQuantity.ToString().Remove(rowQuantity.ToString().IndexOf(:);string Box = arrCookiei.Substring(arrCookiei.LastIndexOf(:) + 1);if (Box = 1)rowBox = true;elserowBox = false;elsedt = Database.GetDataTable(select top 0 * from View_ProductList);dt.Columns.Add(Quantity);return dt;