Cookie+DataList实现购物车

分享是一种美德

--(1)下面是购物车类

namespace XR.Web { using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Web; public class CookieShoppingCart { public CookieShoppingCart() { } /// <summary> /// 将商品信息添加到购物篮 /// </summary> /// <param name="ProductID">商品编号</param> /// <param name="Amount">数量</param> public static void AddShoppingCart(int ProductID, int Amount) { if (HttpContext.Current.Request.Cookies["ShoppingCart"] == null) { HttpCookie MyCookies = new HttpCookie("ShoppingCart"); MyCookies.Expires.AddDays(1); MyCookies.Value = ProductID.ToString() + "*" + Amount.ToString(); HttpContext.Current.Response.Cookies.Add(MyCookies); } else { bool bExists = false; char[] sep = { ',' }; HttpCookie MyCookie = (HttpCookie)HttpContext.Current.Request.Cookies["ShoppingCart"]; MyCookie.Expires = DateTime.Now.AddDays(1); string sProdID = MyCookie.Value.ToString(); string[] arrCookie = sProdID.Split(sep); //查看cookie中是否有该产品 string newCookie = ""; for (int i = 0; i < arrCookie.Length; i++) { // if (arrCookie[i].Length != 0) { if (arrCookie[i].Trim().Remove(arrCookie[i].IndexOf('*')) == ProductID.ToString().Trim()) { bExists = true; //得到数量 string amountStr = arrCookie[i].Trim().Substring(arrCookie[i].Trim().IndexOf('*') + 1); amountStr = (Convert.ToInt32(amountStr) + Amount).ToString(); arrCookie[i] = arrCookie[i].Trim().Remove(arrCookie[i].IndexOf('*')) + "*" + amountStr; newCookie = newCookie + "," + arrCookie[i]; } else { newCookie = newCookie + "," + arrCookie[i]; } } } if (!bExists) { if (MyCookie.Value.Length == 0) { MyCookie.Value = ProductID.ToString() + "*" + Amount.ToString(); } else { MyCookie.Value = MyCookie.Value + "," + ProductID.ToString() + "*" + Amount.ToString(); } } else { MyCookie.Value = newCookie.Substring(1); } HttpContext.Current.Response.Cookies.Add(MyCookie); } } /// <summary> /// 删除相应产品 /// </summary> /// <param name="ProductID"></param> /// <returns></returns> public static int RemoveShoppingCart(int ProductID) { int CookiesTotal = 0; int Result = 0; if (HttpContext.Current.Request.Cookies["ShoppingCart"] == null) { } else { try { HttpCookie oCookie = (HttpCookie)HttpContext.Current.Request.Cookies["ShoppingCart"]; CookiesTotal = oCookie.Value.Length; //Set Cookie to expire in 3 hours char[] sep = { ',' }; oCookie.Expires = DateTime.Now.AddHours(6); //Check if Cookie already contain same item string sProdID = oCookie.Value.ToString(); string[] arrCookie = sProdID.Split(sep); string[] arrCookie2 = new string[arrCookie.Length - 1]; int j = 0; for (int i = 0; i < arrCookie.Length; i++) { if (arrCookie[i].Trim().Remove(arrCookie[i].IndexOf('*')) != ProductID.ToString()) { arrCookie2[j] = arrCookie[i]; j++; } } string sCookieID = ""; for (int i = 0; i < arrCookie2.Length; i++) { sCookieID = sCookieID + arrCookie2[i] + ","; } if (sCookieID.Length > 0) { oCookie.Value = sCookieID.Substring(0, sCookieID.Length - 1); } else { oCookie.Value = ""; } HttpContext.Current.Response.Cookies.Add(oCookie); if (oCookie.Value.Length < CookiesTotal) { Result = 1; } else { } } catch { Console.WriteLine("正在处理..."); } } return Result; } /// <summary> /// 更新产品的数量 /// </summary> /// <param name="ProductID"></param> /// <param name="Account"></param> /// <returns></returns> public static int UpdateShoppingCart(int ProductID, int Account) { int Results = 0; HttpCookie MyCookie = (HttpCookie)HttpContext.Current.Request.Cookies["ShoppingCart"]; int OldAccount = 0; string[] strCookie = MyCookie.Value.Split(','); string newCookie = ""; if (strCookie.Length != 0) { for (int i = 0; i < strCookie.Length; i++) { if (strCookie[i].Trim().Remove(strCookie[i].IndexOf('*')) == ProductID.ToString()) { OldAccount = int.Parse(strCookie[i].Trim().Substring(strCookie[i].IndexOf('*') + 1).ToString()); strCookie[i] = ProductID.ToString() + '*' + Account.ToString(); newCookie = newCookie + "," + strCookie[i]; } else { newCookie=newCookie+","+strCookie[i]; } } } MyCookie.Value = newCookie.Substring(1); HttpContext.Current.Response.Cookies.Add(MyCookie); if (OldAccount != Account) { Results = 1; } return Results; } } }

--(2)商品的更新删除,页面

<script runat="server"> DataTable Mytable = new DataTable(); ProductBLL ProductInfo = new ProductBLL(); protected decimal Total = 0; protected string Member = string.Empty; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } private void BindData() { if (Request.Cookies["ShoppingCart"] != null) { HttpCookie oCookie = (HttpCookie)Request.Cookies["ShoppingCart"]; string sProductID = oCookie.Value.ToString(); if (sProductID.Length == 0) { } else { char[] sep = { ',' }; string[] sArrProdID = sProductID.Split(sep); DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("ID")); dt.Columns.Add(new DataColumn("Title")); dt.Columns.Add(new DataColumn("Total")); dt.Columns.Add(new DataColumn("price")); dt.Columns.Add(new DataColumn("PictureUrl")); int counter = 1; for (int i = 0; i < sArrProdID.Length; i++) { DataTable DT = ProductInfo.GetDataByID(Convert.ToInt32(sArrProdID[i].ToString().Remove(sArrProdID[i].IndexOf('*'))), "laozhuangProduct"); DataRow dr = dt.NewRow(); dr["ID"] = sArrProdID[i].Remove(sArrProdID[i].IndexOf('*')); dr["Title"] = DT.Rows[0]["Title"].ToString(); dr["Total"] = sArrProdID[i].Substring(sArrProdID[i].IndexOf('*') + 1); dr["price"] = DT.Rows[0]["Keyword"].ToString(); int total=int.Parse(sArrProdID[i].Substring(sArrProdID[i].IndexOf('*') + 1)); decimal price = Convert.ToDecimal(DT.Rows[0]["Keyword"].ToString()); Total += Convert.ToDecimal(total * price); dr["PictureUrl"] = DT.Rows[0]["PictureUrl"].ToString(); dt.Rows.Add(dr); counter++; } DataList1.DataSource = dt; DataList1.DataBind(); } } } protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e) { int PID = int.Parse(e.CommandArgument.ToString()); if (CookieShoppingCart.RemoveShoppingCart(PID) > 0) { JS.AlertMsg("删除成功!", this.Page); } else { JS.AlertMsg("删除失败!", this.Page); } BindData(); } public void UpdateShopping(int ProductID, string Acount) { CookieShoppingCart.UpdateShoppingCart(ProductID, int.Parse(Acount)); } protected void TextBox1_TextChanged(object sender, EventArgs e) { TextBox myTextBox = (TextBox)sender; //获得所选行号 int index = Convert.ToInt32(((DataListItem)(myTextBox.NamingContainer)).ItemIndex); //编号 string Num = ((Label)DataList1.Items[index].FindControl("Label1")).Text; //数量 string Acount = ((TextBox)DataList1.Items[index].FindControl("TextBox1")).Text; if (CookieShoppingCart.UpdateShoppingCart(int.Parse(Num), int.Parse(Acount)) > 0) { JS.AlertMsg("更新成功!", this.Page); } BindData(); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值