自己寫的一個asp.net-cookies購物車類

來源:互聯網
上載者:User

  自己寫的一個asp.net 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;

  //文章來源:http://study.pctoday.net.cn/2_ASP.net.aspx

  public class CookieShoppingCart

  {

  /// <summary>

  /// 加入購物車

  /// </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);

  }

  //如果cookie已經存在

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

  //查看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);//得到數量

  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(':')) + "已存在!數量:" + OldQuantity + "<br>");

  //HttpContext.Current.Response.Write(arrCookie[i] + "<br>");

  }

  newCookie = newCookie + "," + arrCookie[i];

  }

  //如果沒有該產品

  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>

  /// 移除購物車子項

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

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

  //查看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;

  }

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.