利用对象序列化将购物车保存在Cookie中

购物车类:ShopCart.cs(说明:主要利用hashtable保存商品对象)

原始地址:  http://tech.it168.com/d/2008-05-08/200805080928738_1.shtml

using System;
using System.Collections;
/// <summary>
/// 购物车类
/// </summary>
[Serializable]
public class ShopCart
{
public Hashtable _CartItems = new Hashtable();
/// <summary>
/// 构造函数
/// </summary>
    public ShopCart()
{
///to do something
    }
/// <summary>
/// 返回购物车中的所有商品(接口类型)
/// </summary>
    public ICollection CartItems
{
get { return _CartItems.Values; }
}
/// <summary>
/// 购物中所有商品的价格合计
/// </summary>
    public double Total
{
get
{
double sum = 0;
foreach (ShopCartItem item in _CartItems.Values)
{
sum += ((item.Price * item.Quantity) + item.SendPrice);
}
return sum;
}
}
/// <summary>
/// 返回购物车里所有商品的数量
/// </summary>
    public double TotalNum
{
get
{
double sum = 0;
foreach (ShopCartItem item in _CartItems.Values)
{
sum += item.Quantity;
}
return sum;
}
}
/// <summary>
/// 向购物车里添加某商品
/// </summary>
/// <param name="ID">商品ID</param>
/// <param name="Name">商品名称</param>
/// <param name="Price">商品单价</param>
/// <param name="AutoAddQuantity">如果购物车中已经存在该商品,该商品的数量是否加一,True数量加1,False数量不变</param>
    public void AddItem(string ID, string Name, string DeliveryName, double Price, int Score, string ProductId, string PicUrl, double MarketPrice, double UserPrice, double VipPrice, double SendPrice,string ShopID, string UrlFrom, bool AutoAddQuantity)
{
ShopCartItem item = (ShopCartItem)_CartItems[ID];
if(item == null)
_CartItems.Add(ID, new ShopCartItem(ID, Name, DeliveryName, Price, Score, ProductId, PicUrl, MarketPrice, UserPrice, VipPrice, SendPrice,ShopID, UrlFrom));
else
{
if(AutoAddQuantity)
{
item.Quantity++;
}
_CartItems[ID] = item;
}
}
/// <summary>
/// 从购物车里移除某商品
/// </summary>
/// <param name="ID">商品ID</param>
/// <param name="FullDelete">如果商品数量大于1,是否彻底从购物车中删除该种商品,true彻底从购物车中删除该种商品,false则仅将该种商品数量减一</param>
    public void RemoveItem(string ID, bool FullDelete)
{
ShopCartItem item = (ShopCartItem)_CartItems[ID];
if(item == null)
{
return;
}
else
{
if(FullDelete)
{
_CartItems.Remove(ID);
}
else
{
item.Quantity--;
if(item.Quantity == 0)
{
_CartItems.Remove(ID);
}
else
{
_CartItems[ID] = item;
}
}
}
}
/// <summary>
/// 修改购物车里某商品的数量
/// </summary>
/// <param name="ID">商品ID</param>
/// <param name="Quantity">商品数量</param>
    public void UpdateItem(string ID, int Quantity)
{
ShopCartItem item = (ShopCartItem)_CartItems[ID];
if(item == null)
{
return;
}
else
{
if(Quantity > 0) //商品数量必须大于0
            {
item.Quantity = Quantity;
}
_CartItems[ID] = item;
}
}
/// <summary>
/// 清空购物车
/// </summary>
    public void ClearCart()
{
_CartItems.Clear();
}
}
/// <summary>
/// [购物车具体]商品类
/// </summary>
[Serializable]
public class ShopCartItem
{
private string _ID;//产品GUID
    private string _Name;//产品名称
    private string _DeliveryName;//物流产品名
    private double _Price=0;//产品单价(结算价格)
    private int _Score = 0;//产品单个积分
    private int _Quantity = 1;//产品数量
    private string _ProductId;//产品自定义编号
    private string _PicUrl;//图片地址
    private double _MarketPrice=0;//市场价格
    private double _VipPrice=0;//VIp价格
    private double _UserPrice=0;//会员价格
    private double _SendPrice=0;//运输费用
    private string _ShopID;//商家ID
    private string _UrlFrom; //页面来源

/// <summary>
/// 属性:商品ID
/// </summary>
    public string ID
{
get { return _ID; }
}
/// <summary>
/// 属性:商品名称Name
/// </summary>
    public string Name
{
get { return _Name; }
}
/// <summary>
/// 属性:商品单价Price
/// </summary>
    public double Price
{
get { return _Price; }
}
/// <summary>
/// 单件产品所获积分
/// </summary>
    public int Score
{
get
{
return _Score;
}
set
{
_Score = value;
}
}
/// <summary>
/// 产品编号
/// </summary>
    public string ProductId
{
get
{
return _ProductId;
}
set
{
_ProductId = value;
}
}
/// <summary>
/// 产品图片地址
/// </summary>
    public string PicUrl
{
get
{
return _PicUrl;
}
set
{
_PicUrl = value;
}
}
/// <summary>
/// 市场价格
/// </summary>
    public double MarketPrice
{
get
{
return _MarketPrice;
}
set
{
_MarketPrice = value;
}
}
/// <summary>
/// VIP价格
/// </summary>
    public double VipPrice
{
get
{
return _VipPrice;
}
set
{
_VipPrice = value;
}
}
/// <summary>
/// 会员价格
/// </summary>
    public double UserPrice
{
get
{
return _UserPrice;
}
set
{
_UserPrice = value;
}
}
/// <summary>
/// 运输费用
/// </summary>
    public double SendPrice
{
get
{
return _SendPrice;
}
set
{
_SendPrice = value;
}
}
/// <summary>
/// 属性:商品数量Quantity
/// </summary>
    public int Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
/// <summary>
/// 商家ID
/// </summary>
    public string ShopID {
get { return _ShopID; }
set { _ShopID = value; }
}
/// <summary>
/// 页面来源
/// </summary>
    public string UrlFrom
{
get { return _UrlFrom; }
set { _UrlFrom = value; }
}
/// <summary>
/// 购物车商品项完整构架函数
/// </summary>
/// <param name="ID">产品ID</param>
/// <param name="Name">产品名称</param>
/// <param name="DeliveryName">产品物流名称</param>
/// <param name="Price">产品单价(计算价)</param>
/// <param name="Score">产品积分</param>
/// <param name="ProductId">产品编号</param>
/// <param name="PicUrl">产品图片</param>
/// <param name="MarketPrice">会员价格</param>
/// <param name="UserPrice">市场价格</param>
/// <param name="VipPrice">VIp价格</param>
/// <param name="SendPrice">运输费用</param>
/// <param name="ShopID">商家ID</param>
/// <param name="UrlFrom">页面来源</param>
    public ShopCartItem(string ID, string Name, string DeliveryName, double Price, int Score, string ProductId, string PicUrl, double MarketPrice, double UserPrice, double VipPrice, double SendPrice, string ShopID, string UrlFrom)
{
_ID = ID;
_Name = Name;
_DeliveryName = DeliveryName;
_Quantity = 1;
_Price = Price;
_Score = Score;
_ProductId = ProductId;
_PicUrl = PicUrl;
_MarketPrice = MarketPrice;
_UserPrice = UserPrice;
_VipPrice = VipPrice;
_SendPrice = SendPrice;
_ShopID = ShopID;
_UrlFrom = UrlFrom;
}
/// <summary>
/// 购物车商品项简单构架函数
/// </summary>
/// <param name="ID">产品ID</param>
/// <param name="Name">产品名称</param>
/// <param name="Price">产品单价(计算价)</param>
    public ShopCartItem(string ID, string Name, double Price)
{
_ID = ID;
_Name = Name;
_DeliveryName = "";
_Quantity = 1;
_Price = Price;
_Score = 0;
_ProductId = "";
_PicUrl = "";
_MarketPrice = 0;
_UserPrice = 0;
_VipPrice = 0;
_SendPrice = 0;
_ShopID = "";
_UrlFrom = "";
}
/// <summary>
/// 购物车商品项构架函数
/// </summary>
/// <param name="ID">产品ID</param>
/// <param name="Name">产品名称</param>
/// <param name="Price">产品单价(计算价)</param>
/// <param name="UrlFrom">页面来源</param>
    public ShopCartItem(string ID, string Name, double Price,string UrlFrom)
{
_ID = ID;
_Name = Name;
_DeliveryName = "";
_Quantity = 1;
_Price = Price;
_Score = 0;
_ProductId = "";
_PicUrl = "";
_MarketPrice = 0;
_UserPrice = 0;
_VipPrice = 0;
_SendPrice = 0;
_ShopID = "";
_UrlFrom = UrlFrom;
}
/// <summary>
/// 购物车商品项标准构架函数
/// </summary>
/// <param name="ID">产品ID</param>
/// <param name="Name">产品名称</param>
/// <param name="Price">产品单价(计算价)</param>
/// <param name="UrlFrom">页面来源</param>
/// <param name="UrlFrom">页面来源</param>
    public ShopCartItem(string ID, string Name, double Price,string ShopID, string UrlFrom)
{
_ID = ID;
_Name = Name;
_DeliveryName = "";
_Quantity = 1;
_Price = Price;
_Score = 0;
_ProductId = "";
_PicUrl = "";
_MarketPrice = 0;
_UserPrice = 0;
_VipPrice = 0;
_SendPrice = 0;
_ShopID = ShopID;
_UrlFrom = UrlFrom;
}
}
测试页面:Demo.aspx
  
  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="Public_Test_Demo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label><br />
<br />
<asp:Button ID="btnReadCookie" runat="server" Text="读取Cookie中的ShopCart" OnClick="btnReadCookie_Click" /> </div>
</form>
</body>
</html>

后置代码Demo.aspx.cs文件:
using System;
using System.Collections;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;
using CNTVS.TOOLS;
public partial class Public_Test_Demo : System.Web.UI.Page
{
string CookieName = "ShopCart";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShopCart SC = new ShopCart();
SC.AddItem("1", "ProductName", "ProductName", 0, 0, "ProductID", "", 0, 0, 0, 0, "ShopId", "TestUrl", true);
SC.AddItem("2", "ProductName123", "ProductName123", 0, 0, "ProductID123", "", 0, 0, 0, 0, "ShopId111", "TestUrl23", true);
//将ShopCart对象写入Cookie
            IFormatter fm = new BinaryFormatter();
Stream sm = new MemoryStream();
fm.Serialize(sm, SC);
sm.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(sm);
string strCart = reader.ReadToEnd();
reader.Close();
HttpCookie hc = new HttpCookie(CookieName);
hc.Value = Server.UrlEncode(strCart);
hc.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(hc);
}
}
/ <summary>
/ 将ShopCart写入Cookie
/ </summary>
/ <param name="SC">ShopCart对象</param>
/ <param name="CookieName">CookieName,默认为ShopCart</param>
    //public static string ShopCartToCookie(ShopCart SC, string CookieName)
//{
//    if (Utils.CheckNull(CookieName)) { CookieName = "ShopCart"; }
//    if (Utils.CheckNull(SC))
//    {
//        Utils.WriteCookie(CookieName, "");
//        return "";
//    }
//    else
//    {
//        IFormatter fm = new BinaryFormatter();
//        Stream sm = new MemoryStream();
//        fm.Serialize(sm, SC);
//        sm.Seek(0, SeekOrigin.Begin);
//        StreamReader reader = new StreamReader(sm);
//        string strCart = reader.ReadToEnd();
//        reader.Close();
//        Utils.WriteCookie(CookieName, strCart);
//        return strCart;
//    }
//}

/ <summary>
/ 将Cookie反序列化为ShopCart
/ </summary>
/ <param name="CookieName">CookieName,默认为ShopCart</param>
    //public static ShopCart CookieToShopCart(string CookieName)
//{
//    if (Utils.CheckNull(CookieName)) { CookieName = "ShopCart"; }
//    string StrCart = Utils.GetCookie(CookieName);
//    if (Utils.CheckNull(StrCart))
//    {
//        return null;
//    }
//    byte[] bt = System.Text.Encoding.Default.GetBytes(StrCart);
//    Stream sm = new MemoryStream(bt);
//    IFormatter fm = new BinaryFormatter();
//    ShopCart SC = (ShopCart)fm.Deserialize(sm);
//    if (Utils.CheckNull(SC))
//    {
//        return null;
//    }
//    else
//    {
//        return SC;
//    }
//}

protected void btnReadCookie_Click(object sender, EventArgs e)
{
string StrCartNew = Server.UrlDecode(Request.Cookies[CookieName].Value.ToString());
byte[] bt = System.Text.Encoding.Default.GetBytes(StrCartNew);
Stream smNew = new MemoryStream(bt);
IFormatter fmNew = new BinaryFormatter();
ShopCart SCNew = (ShopCart)fmNew.Deserialize(smNew);
foreach(ShopCartItem SCI in SCNew.CartItems)
{
lblResult.Text += "<br/>产品名称:" + SCI.Name;
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值