惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 优雅旋律

四角号码 对照表 ScriptManager Bug Application_Start 不执行 loading 关机效果 ZT Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景] ZT 数据库范式浅解 ZT 数据库设计系列:数据库设计5步骤 ZT Web Control 开发系列(一) 页面的生命周期 MarcHandler (Marc ISO2709) Resharper 4.0 终于RC了... GridView、DetailsView、FormView 、Repeater、DataList的区别 (ZT) 常用正则表达式 ZT linq(update) 更新用 自动赋值方法 ajax tab 动态设置主题(Theme) 一些注意 持久化数据 关于vs2008的Eval方法 重大失误 datapager分页问题 (点击两次)
利用对象序列化将购物车保存在Cookie中 (ZT)
优雅旋律 · 2008-04-27 · via 博客园 - 优雅旋律

购物车类:ShopCart.cs(说明:主要利用hashtable保存商品对象)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" />&nbsp;</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"00"ProductID"""0000"ShopId""TestUrl"true);
            SC.AddItem(
"2""ProductName123""ProductName123"00"ProductID123"""0000"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;
        }
    }
}