本文转自:http://www.sulong.cc/article/program/aspx/110613114249.html

#region 添加到购物车AddShoppingCar
/// <summary>
/// 添加到购物车AddShoppingCar
/// </summary>
/// <param name="num">数量 如果存在产品 负数是减少
/// 正数是增加 如果不存在 直接增加</param>
/// <param name="id">货物ID</param>
/// <param name="expires">cookies保存的天数</param>
/// <remarks>这里的方法就是把在原有的Cookie基础上判断是否有
/// 这个产品 如果有 在原有数量上增加 没有 就直接增加 如果是负
/// 数 就是减少 如果负数的数量大于等于
/// 原有数量 设置为0 对应后面的读出操作</remarks>
public static void AddShoppingCar(string num, string id, int expires)
{
if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null)
{
System.Web.HttpCookie cookie;
string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
if (System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()] == null)
{
cookievalue = cookievalue + "&" + id + "=" + num; }
else
{
int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()].ToString()) + int.Parse(num);
if (num1 > )
{
System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()] = num1.ToString();
}
else
{
System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()] = "";
}
cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
}
cookie = new System.Web.HttpCookie("Products", cookievalue);
if (expires != )
{
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(expires, , , );
cookie.Expires = dt.Add(ts);
}
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
else
{
System.Web.HttpCookie newcookie = new HttpCookie("Products");
if (expires != )
{
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(expires, , , );
newcookie.Expires = dt.Add(ts);
}
newcookie.Values[id.ToString()] = num;
System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
}
}
#endregion #region 添加到购物车AddShoppingCar
/// <summary>
/// 添加到购物车AddShoppingCar
/// </summary>
/// <param name="num">数量 如果存在产品 负数是减少
/// 正数是增加 如果不存在 直接增加</param>
/// <param name="dt">
/// 循环读出来的DATATABLE
/// </param>
/// <param name="id">货物ID</param>
/// <param name="expires">cookies保存的天数</param>
/// <remarks>这里的方法就是把在原有的Cookie基础上判断是否有
/// 这个产品 如果有 在原有数量上增加 没有 就直接增加 如果是负
/// 数 就是减少 如果负数的数量大于等于
/// 原有数量 设置为0 对应后面的读出操作</remarks>
public static void AddShoppingCar(DataTable dt, int expires)
{ if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null)
{
System.Web.HttpCookie cookie;
string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
for (int i = ; i < dt.Rows.Count; i++)
{
if (System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()] == null)
{
cookievalue = cookievalue + "&" + dt.Rows[i]["id"].ToString() + "=" + dt.Rows[i]["num"].ToString();
}
else
{
int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()].ToString()) + int.Parse(dt.Rows[i]["num"].ToString());
if (num1 > )
{
System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()] = num1.ToString();
}
else
{
System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()] = "";
}
cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
}
}
cookie = new System.Web.HttpCookie("Products", cookievalue);
if (expires != )
{
DateTime time1 = DateTime.Now;
TimeSpan ts = new TimeSpan(expires, , , );
cookie.Expires = time1.Add(ts);
}
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
else
{
System.Web.HttpCookie newcookie = new HttpCookie("Products");
if (expires != )
{
DateTime time1 = DateTime.Now;
TimeSpan ts = new TimeSpan(expires, , , );
newcookie.Expires = time1.Add(ts);
}
for (int i = ; i < dt.Rows.Count; i++)
{
newcookie.Values[dt.Rows[i]["id"].ToString()] = dt.Rows[i]["num"].ToString();
}
System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
} }
#endregion #region 根据ID删除产品RemoveShoppingCar
/// <summary>
/// 根据ID删除产品RemoveShoppingCar
/// </summary>
/// <param name="id">产品ID</param>
/// <remarks>
/// 就是设置商品数量为0
/// 本来增加方法可以实现
/// 但是需要读出来原有数量
/// 所以为了避免繁琐 有此方法
/// </remarks>
public static void RemoveShoppingCar(string id)
{
if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] != null)
{
System.Web.HttpCookie cookie;
System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = "";
string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
cookie = new System.Web.HttpCookie("Products", cookievalue);
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
}
#endregion #region 删除购物车RemoveShoppingCar
/// <summary>
/// 删除购物车RemoveShoppingCar
/// </summary>
/// <remarks>
/// 使购物车的Cookie为空
/// </remarks>
public static void RemoveShoppingCar()
{
if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values.Count != )
{
System.Web.HttpContext.Current.Request.Cookies["Products"].Expires = System.DateTime.Now.AddHours(-);
System.Web.HttpContext.Current.Response.AppendCookie(System.Web.HttpContext.Current.Request.Cookies["Products"]);
}
}
#endregion #region 根据ID修改产品UpdateShoppingCar
/// <summary>
/// 根据ID修改产品UpdateShoppingCar
/// </summary>
/// <param name="id">产品ID</param>
/// <param name="num">产品数量</param>
/// <remarks>
/// 更新产品的数量
/// </remarks>
public static void UpdateShoppingCar(string id,string num)
{
if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] != null)
{
System.Web.HttpCookie cookie;
System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = num;
string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
cookie = new System.Web.HttpCookie("Products", cookievalue);
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
}
}
#endregion #region 得到所有的产品列表GetAllChoppingCar
/// <summary>
/// 得到所有的产品列表GetAllChoppingCar
/// </summary>
/// <returns>所有产品的数据集</returns>
/// <remarks>因为对DataTable操作比较方便(个人喜好)
/// 这里是把里面的字符串分割为Datatable</remarks>
public static DataTable GetAllChoppingCar()
{
if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Value.Trim() != "")
{
DataColumn dcid = new DataColumn("id");
DataColumn dcnum = new DataColumn("num");
DataTable dt = new DataTable();
dt.Columns.Add(dcid);
dt.Columns.Add(dcnum);
string[] str = System.Web.HttpContext.Current.Request.Cookies["Products"].Value.Split("&");
for (int i = ; i < str.Length; i++)
{
DataRow dr = dt.NewRow();
dr["id"] = (str[i].Split("="))[].ToString();
dr["num"] = (str[i].Split("="))[].ToString();
if (int.Parse((str[i].Split("="))[].ToString()) != )
{
dt.Rows.Add(dr);
}
}
return dt;
}
else
{
return null;
}
}
#endregion #region 关于购物车Cookie里的DataTable的操作GetCookieByDataTable
/// <summary>
/// 关于购物车Cookie里的DataTable的操作GetCookieByDataTable
/// </summary>
/// <param name="dt"></param>
/// <remarks>
/// 把读出来的DataTable转换为字符串
/// 根据的规则是自己定的 &符号是分开产品
/// =号是分开产品ID和产品价格
/// </remarks>
/// <returns></returns>
public static String GetCookieByDataTable(DataTable dt)
{
String datatable = "";
if (dt.Rows.Count > )
{
for (int i = ; i < dt.Rows.Count; i++)
{
datatable = dt.Rows[i]["id"].ToString() + "=" + dt.Rows[i]["num"].ToString() + "&" + datatable;
}
}
return datatable;
}
#endregion #region 把字符串转换为datatable GetDataTable
/// <summary>
/// 把字符串转换为datatable GetDataTable
/// </summary>
/// <param name="datatable"></param>
/// <remarks>
/// 把Cookie里的字符串转换为DataTable
/// 是GetCookieByDataTable的反操作
/// </remarks>
/// <returns></returns>
public static DataTable GetDataTable(string datatable)
{
DataColumn dcid = new DataColumn("id");
DataColumn dcnum = new DataColumn("num");
DataTable dt = new DataTable();
dt.Columns.Add(dcid);
dt.Columns.Add(dcnum);
if (!datatable.StartsWith("&"))
{
string[] str = datatable.Split("&");
for (int i = ; i < str.Length; i++)
{
DataRow dr = dt.NewRow();
dr["id"] = (str[i].Split("="))[].ToString();
dr["num"] = (str[i].Split("="))[].ToString();
if (int.Parse((str[i].Split("="))[].ToString()) != )
{
dt.Rows.Add(dr);
}
}
}
else
{
DataRow dr = dt.NewRow();
dr["id"] = (datatable.Split("="))[].ToString();
dr["num"] = (datatable.Split("="))[].ToString();
if (int.Parse((datatable.Split("="))[].ToString()) != )
{
dt.Rows.Add(dr);
}
}
return dt;
}
#endregion

[转].net cookie版购物车的更多相关文章

  1. Cookie 版购物车

    写一个JS文件  把相应的方法写在JS文件内 为了方便以后的调用 具体代码为 var Cart = function () { this.Count = 0; this.Total = 0; this ...

  2. 购物车非cookie版

    2015.11.26购物车,非cookie版 [点击来,你发现被骗了(笑哭,笑哭,笑哭,源代码的话,留下邮箱吧,是在不好找这一时半会儿的.)] Jsp通过反射机制获取bean中的标签,但其实,可以没有 ...

  3. 【JSP】Cookie的使用及保存中文,并用Cookie实现购物车功能

    Cookie是服务器存放在客户端的一些数据,比如密码,以及你曾经访问过的一些数据. 设置Cookie //设置cookie Cookie cookie = new Cookie("TOM&q ...

  4. 工作任务:模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能

    登入界面<% Cookie[] cks =request.getCookies(); String str=null; for(Cookie ck:cks) { if(ck.getName(). ...

  5. 基于Cookie的购物车

    var Cookies = {}; Cookies.set = function (name, value) { var argv = arguments; var argc = arguments. ...

  6. jQuery基于json与cookie实现购物车的方法

    /** * 添加商品及数量到购物车cookie中,返回当前商品在cookie中的总数 */ function AddToShoppingCar(id, num, type) { var _num = ...

  7. Cookie实现购物车功能

    这里的购物车暂时存放书,后期把参数改成Object,把方法抽取成接口,只要实现了接口的Object类都可以放进购物项,这样就实现了购物任何物品 使用购物项因为一个购物项可以包含某种商品的数量,总价等, ...

  8. javaweb学习——session和Cookie实现购物车功能

    1.创建Book类,实现对图书信息的封装. package cn.it.sessionDemo.example1; import java.io.Serializable; /** * 该类实现对图书 ...

  9. 【Tomcat】JSP使用Session、Cookie实现购物车

    购物界面shop.jsp 初始页面 添加商品后,在session中设置属性,重定向回到shop.jsp,然后根据session的内容显示结果 Cookie设置setMaxAge可以延长session的 ...

随机推荐

  1. 组队训练1 回放(转载至cxhscst2's blog)

      第一场组队训练……意料之中的爆炸. 开场先看题,H是斐波那契水题,qw把H切了. 同时czy看I题(排列),cst继续读其他题. czy尝试交I,PE. cst发现K是水题. cst上来敲K,WA ...

  2. HTTPS 是如何保证安全的?

    每当我们讨论到信息安全的时候,我们最长接触到的信息加密传输的方式莫过于 HTTPS 了,当我们浏览器地址栏闪现出绿色时,就代表着这个网站支持 HTTPS 的加密信息传输方式,并且你与它的连接确实被加密 ...

  3. MongoDB小结24 - 索引简介2

    索引的名字 集合中每个索引都有一个字符串类型的名字,来唯一标识索引. 服务器通过名字来操作或者删除索引. 要注意的是,索引名有字符个数限制,所以索引创建时一定要用自定义的名字,如 db.user.en ...

  4. 【python】glob模块、os模块

    http://www.cnblogs.com/hongten/p/hongten_python_glob.html http://wenku.baidu.com/link?url=AgUq9_yQVj ...

  5. FTP指令说明

    安装vsftpd: listen=YES: 是否监听端口 anonymous_enable=NO: 是否启用匿名用户 local_enable=YES: 是否允许本地用户登录 write_enable ...

  6. Raphael.js API 之Element.remove(),Element.removeData(),paper.text(),Element.node(),Element.onDragOver

    /*API-38*/ Element.remove() 删除某个元素对象,无返回值 /*API-39*/ Element.removeData([key]); 删除某个key的value值.假设没有特 ...

  7. 【图像处理】基于OpenCV底层实现的图片旋转

    image processing 系列 [图像处理]直方图匹配 [图像处理]高斯滤波.中值滤波.均值滤波 图片旋转,本质上是对旋转后的图片中每一个像素点计算在原图的位置.然后照搬过来就好. (多说一句 ...

  8. 99_leetcode_Best Time to Buy and sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  9. 菜鸟的mongoDB学习---(五)MongoDB的limit、skip、sort方法

    limit方法 假设你须要在MongoDB中读取指定数量的数据记录.能够使用MongoDB的Limit方法,limit()方法接受一个数字參数,该參数指定从MongoDB中读取的记录条数. mongo ...

  10. String,StringBuffer,StringBuilder三者有什么异同?

    相同点: 1.三者都是Java平台提供的三种类型得到字符串,它们可以储存和操作字符串. 不同点: 1.String是final修饰的,也就意味着String引用的字符串内容是不能被改变的.而Strin ...