一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net cookies实现购物车代码

时间:2011-06-13 编辑:简简单单 来源:一聚教程网

cookies的购物车类

先来看一下简单cookie应用

在 Cookies 集合中设置属性来写入 Cookie
在你想要写入 Cookie 的 ASP.NET 页面中,在 Cookies 集合中指定 Cookie 的属性。

如下代码实例说明了一个名为 UserSettings 的 Cookie,并为子键 Font 和 Color 设置了值。同时也把失效时间设置成了明天。

Response.Cookies["UserSettings"]["Font"] = "Arial";
Response.Cookies["UserSettings"]["Color"] = "Blue";
Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(1d);

创建 HttpCookie 对象的实例来写入 Cookie
创建 HttpCookie 类型的一个对象实例并为其指定名称。

指定 Cookie 子键中的值并设置 Cookie 的属性。

把这个 Cookie 添加到 Cookies 集合中。

如下代码实例说明了一个名为 myCookie 的 HttpCookie 对象实例,用来展示一个名为 UserSettings 的 Cookie。

HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);

 

ASP.NET 实践:读取 Cookie
Cookies 提供了一种在 Web 应用程序中存储特定用户信息(如历史记录或用户偏好)的方式。Cookie 是连同请求和回应一起在 Web 服务器和客户端之间来回传送的少量文本。Web 应用程序能够在用户访问网站的时候读取 Cookie 中所包含的信息。

浏览器负责对用户系统中的 Cookies 进行管理。Cookies 连同页面请求一起被发送到服务器,并作为 HttpRequest 对象中的一部分而能够被访问,并暴露了一个 Cookies 集合。你只能够读取当前域或路径中的页面所创建的 Cookies。


读取 Cookie
把 Cookie 的名称作为关键字从 Cookies 中读取一个字符串。

如下实例读取了一个名为 UserSettings 的 Cookie,然后读取名为 Font 的子键的值。

if (Request.Cookies["UserSettings"] != null)
{
    string userSettings;
    if (Request.Cookies["UserSettings"]["Font"] != null)
    { userSettings = Request.Cookies["UserSettings"]["Font"]; }
}

 

ASP.NET 实践:删除 Cookie
你不能直接删除用户计算机中的 Cookie。但是,你能够通过把 Cookie 的有效日期设置成一个已经过去的日期来指挥用户浏览器对 Cookie 进行删除。用户在下一次对设置在 Cookie 中的域或路径中的页面进行访问的时候,浏览器会检测并删除已经过期的 Cookies。

提示:调用 Cookies 集合的 Remove 方法可以在服务器端把 Cookie 从集合中删除,但是 Cookie 将不会被发送到客户端。因此,这个方法不会从客户端删除已经存在的 Cookie。

为 Cookie 指派一个已经过去的日期
检测 Cookie 是否存在,如果存在,创建一个拥有相同名称的新 Cookie。

把新 Cookie 的有效日期设置成一个已经过去的时间。

把这个新 Cookie 添加到 Cookies 集合对象中。

如下代码实例说明了如何为 Cookie 设置一个已经过去的日期。

if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
}

 


再看详细的购物车实例

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;
    //文章来源:http://www.111com.net
    public class CookieShoppingCart
    {
    ///
    /// 加入购物车
    ///
    ///
    ///
    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 + "
    ");
    //HttpContext.Current.Response.Write(arrCookie[i] + "
    ");
    }
    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);
    }
    }
///
    /// 移除购物车子项
    ///
    ///
    public static void RemoveShoppingCart(int ProductID)
    {
    if (HttpContext.Current.Request.Cookies["ShoppingCart"] != null)
    {
    HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.Cookies["ShoppingCart"];
    oCookie.Expires = DateTime.Now.AddYears(3);
    //Check if Cookie already contain same item
    string ShoppingCartStr = oCookie.Value.ToString();
    string[] arrCookie = ShoppingCartStr.Split(new char[] { ',' });
    string[] arrCookie2 = new string[arrCookie.Length - 1];
    int j = 0;
    string NewStr = "";
    for (int i = 0; i < arrCookie.Length; i++)
    {
    if (arrCookie[i].Trim().Remove(arrCookie[i].IndexOf(':')) != ProductID.ToString())
    NewStr = NewStr + "," + arrCookie[i];
    }
    if (NewStr == "")
    HttpContext.Current.Response.Cookies["ShoppingCart"].Value = "";
    else
    HttpContext.Current.Response.Cookies["ShoppingCart"].Value = NewStr.Substring(1);
    }
    }
    public static void UpdateShoppingCart(int ProductID, int Quantity, bool box)
    {
    int Box = 1;
    if (!box)
    Box = 0;
    if (HttpContext.Current.Request.Cookies["ShoppingCart"] != null)
    {
    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())
    arrCookie[i] = arrCookie[i].Trim().Remove(arrCookie[i].IndexOf(':')) + ":" + Quantity.ToString() + ":" + Box.ToString();
    newCookie = newCookie + "," + arrCookie[i];
    }
    HttpContext.Current.Response.Cookies["ShoppingCart"].Value = newCookie.Substring(1);
    }
    }
    public static DataTable GetShoppingCart()
    {
    DataTable dt = new DataTable();
    if (HttpContext.Current.Request.Cookies["ShoppingCart"] != null && HttpContext.Current.Request.Cookies["ShoppingCart"].Value.Trim() != "")
    {
    HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.Cookies["ShoppingCart"];
    oCookie.Expires = DateTime.Now.AddYears(3);
    string ShoppingCartStr = oCookie.Value.ToString();
    //HttpContext.Current.Response.Write(ShoppingCartStr);
    string[] arrCookie = ShoppingCartStr.Split(new char[] { ',' });
    //查看cookie中是否有该产品
    string newCookie = "";
    for (int i = 0; i < arrCookie.Length; i++)
    {
    newCookie = newCookie + "," + arrCookie[i].Trim().Remove(arrCookie[i].IndexOf(':'));
    }
    newCookie = newCookie.Substring(1);
    dt = Product.GetProductByProductIds(newCookie, -1);
    dt.Columns.Add("Quantity");
    dt.Columns.Add("Box");
    foreach (DataRow row in dt.Rows)
    {
    for (int i = 0; i < arrCookie.Length; i++)
    {
    if (arrCookie[i].Trim().Remove(arrCookie[i].IndexOf(':')) == row["ProductId"].ToString())
    {
    row["Quantity"] = arrCookie[i].Substring(arrCookie[i].IndexOf(":") + 1);
    row["Quantity"] = row["Quantity"].ToString().Remove(row["Quantity"].ToString().IndexOf(":"));
    string Box = arrCookie[i].Substring(arrCookie[i].LastIndexOf(":") + 1);
    if (Box == "1")
    row["Box"] = true;
    else
    row["Box"] = false;
    }
    }
    }
    }
    else
    {
    dt = Database.GetDataTable("select top 0 * from View_ProductList");
    dt.Columns.Add("Quantity");
    }
    return dt;
    }
    }

热门栏目