asp.net-cookies购物车类

asp.net-cookies购物车类
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;

  }

  }

 

时间: 2024-09-26 12:11:30

asp.net-cookies购物车类的相关文章

asp.net cookies实现购物车代码

cookies的购物车类 先来看一下简单cookie应用 在 Cookies 集合中设置属性来写入 Cookie 在你想要写入 Cookie 的 ASP.NET 页面中,在 Cookies 集合中指定 Cookie 的属性. 如下代码实例说明了一个名为 UserSettings 的 Cookie,并为子键 Font 和 Color 设置了值.同时也把失效时间设置成了明天. Response.Cookies["UserSettings"]["Font"] = &quo

ASP.NET开发购物推车之购物车类

asp.net|购物车 Web应用程序开发中,大多数的电子商务网站都有网上购物这一功能模块,所以购物车程序的编写就显得非常重要. 购物车的作用不外就是实现这些功能:添加物件.修改物件.删除物件.检查推车.查看推车等.在本文就讲解"添加物件"."删除物件"和"查看推车"这三个功能.当然,这里只不过是一个简单的购物车类,完成的功能也不多,还不够完善,需要大家在这个基础做扩展使它的功能更加完善. C#是一种完全的OOP(Object Oriented

[ASP.NET开发系列] 购物推车程序设计——购物车类

asp.net|程序|购物车|设计 Web应用程序开发中,大多数的电子商务网站都有网上购物这一功能模块,所以购物车程序的编写就显得非常重要. 购物车的作用不外就是实现这些功能:添加物件.修改物件.删除物件.检查推车.查看推车等.在本文就讲解"添加物件"."删除物件"和"查看推车"这三个功能.当然,这里只不过是一个简单的购物车类,完成的功能也不多,还不够完善,需要大家在这个基础做扩展使它的功能更加完善. C#是一种完全的OOP(Object Ori

一个比较完善的购物车类

比较|购物车      前不久做到一个项目需要用到购物车,考虑到可能经常用到,所以把它封装成一个类,以便以后调用.你可以简单的把这个类稍微修改一下就可以用在自己的程序里了,具体使用请见http://bigeagle.wotoo.com/article.asp?type=1. <?/*****************************************************************************//*                             

php Cookies操作类

Cookies 操作类 功能: 1.保存,读取,更新,清除cookies数据. 2.可设置前缀. 3.强制超时控制. 4.cookies数据可以是字符串,数组,对象等. Cookies.class.php <?php /** Cookies class 保存,读取,更新,清除cookies数据.可设置前缀.强制超时.数据可以是字符串,数组,对象等. * Date: 2013-12-22 * Author: fdipzone * Ver: 1.0 * * Func: * public set 设置

PHP购物车类

  PHP购物车类: 因为需求中只记录产品的类型,还有编号..比如食物,,然后什么编号,,这样子.所以没有记录产品的数量..要用的自己改进一下就行了. //setcookie("cart","s:26,45,4,4523423|d:17,7,27,26|e:12,13,123,43|f:34"); //cartAdd('e','167'); //getProInfo('z'); //delProInfo('f','26'); /** * *********** *

使用asp.net模拟购物车

问题描述 使用asp.net模拟购物车添加商品怎么做啊,初学不懂,请各位高手指导!!! 解决方案 解决方案二:Session+list<商品类>

php session 购物车类

  <?php class Cart{ public function Cart() { if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); } } /* 添加商品 param int $id 商品主键 string $name 商品名称 float $price 商品价格 int $num 购物数量 */ public function addItem($id,$name,$price,$num,$img) { //如果该商品

asp分页的一个类

分页 asp分页的一个类, 在50,000条记录下测试过,速度比ado的那个要快多了 <% '************************************************************************************'具体用法'Set conn=Server.CreateObject("ADODB.Connection")'conn.open "DRIVER={SQL Server};SERVER=(local);UID=s