自己写的一个a.net-cookies购物车类_.NET概论及软件使用_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

自己写的一个a.net-cookies购物车类

作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0

本篇关键词:购物车一个自己using
黑客防线网安网讯:   自己写的一个asp.net cookies 购物车类,实现购物车功能,实现无需注册就可购物   using System;  using System.Data;  using System.Configuration;  using System.Web;  usi...

   自己写的一个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;

 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

 

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14155-1.html
网站维护教程更新时间:2012-03-30 05:29:30  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer