自己寫的一個 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;
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11694.html