自己写的一个 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;
}
}