Asp.net-cookies Shopping Cart Class

Source: Internet
Author: User
Tags httpcontext set cookie trim

Asp.net-cookies Shopping Cart Class
asp.net cookies shopping cart, realize shopping cart function, realize no need to register to shopping
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;

Article Source: http://study.pctoday.net.cn/2_ASP.net.aspx

public class Cookieshoppingcart

{

<summary>

Add 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[] {', '});

See if the product is in a 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)//Get 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 the product is not
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 Shopping cart Items

</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[] {', '});

See if the product is in a 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[] {', '});

See if the product is in a 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 * view_productlist");

Dt. Columns.Add ("Quantity");

}

return DT;

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.