基于COOKIE技术实现的购物车类

本文介绍了一个购物车管理类的实现,包括添加、删除商品,更改数量等功能,并使用了Cookie来保存购物车信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Collections.Specialized;

namespace EWSLSite.Bll
{
    
/// <summary>
    
/// 购物车
    
/// </summary>

    public  class EWSLShop_ShopCart
    
{
        
/// <summary>
        
/// 放进购物车
        
/// </summary>
        
/// <param name="ID">产品编号</param>

        public void Add(string ID)
        
{
            
//读区购物车COOKIE信息
            NameValueCollection nv = GetCart();
            
if (string.IsNullOrEmpty(nv[ID]))
            
{
                
//不存在,添加
                nv.Add(ID, "1");
            }

            
else
            
{
                
//存在,数量加一
                int n = int.Parse(nv[ID]);
                n
++;
                nv.Set(ID, n.ToString());
            }

            Save(nv);
        }

        
/// <summary>
        
/// 从购物车中去除商品
        
/// </summary>
        
/// <param name="ID"></param>

        public void Del(string ID)
        
{
            NameValueCollection nv 
= GetCart();
            nv.Remove(ID);
            Save(nv);
        }

        
/// <summary>
        
/// 清空购物车
        
/// </summary>

        public void Clear()
        
{
            HttpContext.Current.Response.Cookies[
"CartList"].Value = "";
        }

        
/// <summary>
        
/// 获取购物车中的商品总数
        
/// </summary>
        
/// <returns></returns>

        public int GetNum()
        
{
            
int n = 0;
            n 
= GetCart().Keys.Count;
            
return n;
        }

        
/// <summary>
        
/// 更改订购数量
        
/// </summary>

        public void SetNum(string ID, int Num)
        
{
            NameValueCollection nv 
= GetCart();
            
if (string.IsNullOrEmpty(nv[ID]))
            
{
                nv.Add(ID, Num.ToString());
            }

            
else
            
{
                nv.Set(ID, Num.ToString());
            }


            Save(nv);
        }

        
/// <summary>
        
/// 获取定单列表
        
/// </summary>
        
/// <returns></returns>

        public DataTable GetList()
        
{
            NameValueCollection nv 
= GetCart();
            
string IDs = "";
            
for (int i = 0; i < nv.Count; i++)
            
{
                
if (IDs.Length > 0)
                
{
                    IDs 
+= "," + nv.GetKey(i);
                }

                
else
                
{
                    IDs 
+= nv.GetKey(i);
                }

            }

            EWSLShop_Product handle 
= new EWSLShop_Product();
            DataTable dt 
= handle.GetProInfo(IDs);
            dt.Columns.Add(
"Num"typeof(int));  //数量
            dt.Columns.Add("Sum"typeof(string));  //小计金额
            dt.Columns.Add("ID"typeof(int));  //排序
            dt.Columns.Add("price"typeof(string));
            
for (int i = 0; i < dt.Rows.Count; i++)
            
{
                
string PID = dt.Rows[i]["PID"].ToString();
                
if (string.IsNullOrEmpty(nv[PID]))  //获取产品的数量
                {
                    dt.Rows[i][
"Num"= 1;
                }

                
else
                
{
                    dt.Rows[i][
"Num"= nv[PID];
                }

                dt.Rows[i][
"ID"= GetNvIndex(nv, PID);

                
decimal sum = Convert.ToDecimal(dt.Rows[i]["price"]) * Convert.ToInt32(dt.Rows[i]["Num"]);
                dt.Rows[i][
"Sum"= sum.ToString("c");
            }

            dt.DefaultView.Sort 
= "ID";
            
return dt;
        }

        
/// <summary>
        
/// 从COOKIE读取购物车信息
        
/// </summary>
        
/// <returns></returns>

        private NameValueCollection GetCart()
        
{
            NameValueCollection nv 
= new NameValueCollection();  //商品 ID、购买数量
            if (HttpContext.Current.Request.Cookies["CartList"!= null)
            
{
                nv 
= HttpContext.Current.Request.Cookies["CartList"].Values;
            }

            
return nv;
        }

        
private int GetNvIndex(NameValueCollection nv, string Key)
        
{
            
for (int i = 0; i < nv.Keys.Count; i++)
            
{
                
if (nv.GetKey(i) == Key)
                
{
                    
return i;
                }

            }

            
return -1;   //没有找到
        }

        
/// <summary>
        
/// 将信息写入COOKIE
        
/// </summary>
        
/// <param name="nv"></param>

        private void Save(NameValueCollection nv)
        
{
            HttpCookie cookie 
= new HttpCookie("CartList");
            cookie.Expires 
= DateTime.Now.AddDays(31);
            
///将信息写到添加COOKIE中
            foreach (string key in nv.AllKeys)
            
{
                cookie[key] 
= nv[key];
            }

            HttpContext.Current.Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("gb2312");
            HttpContext.Current.Response.Cookies.Set(cookie);
        }

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值