Write your ownASP. Net cookies shopping cart class, to achieve the shopping cart function, to achieve the shopping without registration
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;
//ArticleSource: http://study.pctoday.net.cn/2_ASP.net.aspx
Public class cookieshoppingcart
{
/// <Summary>
/// Add to shopping cart
/// </Summary>
/// <Param name = "productid"> </param>
/// <Param name = "quantity"> </param>
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 3 hours
Ocookie. expires = datetime. Now. addyears (3 );
Ocookie. value = productid. tostring () + ":" + quantity. tostring () + ":" + box. tostring ();
Httpcontext. Current. response. Cookies. Add (ocookie );
}
// If the cookie already exists
Else
{
Bool bexists = false;
Httpcookie ocookie = (httpcookie) httpcontext. Current. Request. Cookies ["shoppingcart"];
Ocookie. expires = datetime. Now. addyears (3 );
String shoppingcartstr = ocookie. value. tostring ();
String [] arrcookie = shoppingcartstr. Split (New char [] {','});
// Check whether the product exists in the cookie
String newcookie = "";
For (INT I = 0; 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 (':') + 1); // obtain the quantity
Oldquantity = oldquantity. Remove (oldquantity. lastindexof (":"));
Oldquantity = (convert. toint32 (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 (':') + "already exists! Quantity: "+ oldquantity +" <br> ");
// Httpcontext. Current. response. Write (arrcookie [I] + "<br> ");
}
Newcookie = newcookie + "," + arrcookie [I];
}
// If this product does not exist
If (! Bexists)
{
Ocookie. value = ocookie. Value + "," + productid. tostring () + ":" + quantity. tostring () + ":" + box. tostring ();
}
Else
{
Ocookie. value = newcookie. substring (1 );
}
Httpcontext. Current. response. Cookies. Add (ocookie );
Httpcontext. Current. response. Write ("shoppingcart:" + httpcontext. Current. Request. Cookies ["shoppingcart"]. value );
}
}
/// <Summary>
/// Remove the subitem of the shopping cart
/// </Summary>
/// <Param name = "productid"> </param>
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 (3 );
// Check if cookie already contain same item
String shoppingcartstr = ocookie. value. tostring ();
String [] arrcookie = shoppingcartstr. Split (New char [] {','});
String [] arrcookie2 = new string [arrcookie. Length-1];
Int J = 0;
String newstr = "";
For (INT I = 0; 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 (1 );
}
}
Public static void updateshoppingcart (INT productid, int quantity, bool box)
{
Int box = 1;
If (! Box)
Box = 0;
If (httpcontext. Current. Request. Cookies ["shoppingcart"]! = NULL)
{
Bool bexists = false;
Httpcookie ocookie = (httpcookie) httpcontext. Current. Request. Cookies ["shoppingcart"];
Ocookie. expires = datetime. Now. addyears (3 );
String shoppingcartstr = ocookie. value. tostring ();
String [] arrcookie = shoppingcartstr. Split (New char [] {','});
// Check whether the product exists in the cookie
String newcookie = "";
For (INT I = 0; 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 (1 );
}
}
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 (3 );
String shoppingcartstr = ocookie. value. tostring ();
// Httpcontext. Current. response. Write (shoppingcartstr );
String [] arrcookie = shoppingcartstr. Split (New char [] {','});
// Check whether the product exists in the cookie
String newcookie = "";
For (INT I = 0; I <arrcookie. length; I ++)
{
Newcookie = newcookie + "," + arrcookie [I]. Trim (). Remove (arrcookie [I]. 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 (arrcookie [I]. Trim (). Remove (arrcookie [I]. indexof (':') = row ["productid"]. tostring ())
{
Row ["quantity"] = arrcookie [I]. substring (arrcookie [I]. indexof (":") + 1 );
Row ["quantity"] = row ["quantity"]. tostring (). Remove (row ["quantity"]. tostring (). indexof (":"));
String box = arrcookie [I]. substring (arrcookie [I]. lastindexof (":") + 1 );
If (box = "1 ")
Row ["box"] = true;
Else
Row ["box"] = false;
}
}
}
}
Else
{
Dt = database. getdatatable ("select top 0 * From view_productlist ");
DT. Columns. Add ("quantity ");
}
Return DT;
}
}