- Rongsen.Com.Cn 版权所有 2008-2010 京ICP备08007000号 京公海网安备11010802026356号 朝阳网安编号:110105199号
- 北京黑客防线网安工作室-黑客防线网安服务器维护基地为您提供专业的
服务器维护
,企业网站维护
,网站维护
服务 - (建议采用1024×768分辨率,以达到最佳视觉效果) Powered by 黑客防线网安 ©2009-2010 www.rongsen.com.cn
作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0 |
自己写的一个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
我要申请本站:N点 | 黑客防线官网 | |
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479 |