ASP.NET session 版购物车,哪位大侠帮忙写一下

 我来答
红星路柯基犬Dc7ad
2011-11-21 · TA获得超过134个赞
知道小有建树答主
回答量:139
采纳率:0%
帮助的人:156万
展开全部
首先,LZ需要一个计算逻辑的类,下面是代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;

namespace siteadmin
{
[Serializable]
public class CartItem
{
private int _productID;
private string _productName;
private int _quantity;
private decimal _price;
private decimal _lineTotal;
public int ProductID
{
get { return _productID; }
set { _productID = value; }
}
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
public decimal Price
{
get { return _price; }
set { _price = value; }
}
public decimal LineTotal
{
get { return _quantity * _price; }
}

public void New(int productID, string productName, int quantity, decimal price)
{
_productID = productID;
_productName = productName;
_quantity = quantity;
_price = price;
_lineTotal = Quantity * Price;
}
}

public class ShoppingCart
{
private DateTime _dateCreated;
private DateTime _lastUpdate;
private List<CartItem> _items;
public List<CartItem> Items
{
get { return _items; }
set { _items = value; }
}
public decimal Total
{
get
{
decimal t = 0;
if (_items == null)
{
return 0;
}
foreach (CartItem item in _items)
{
t += item.LineTotal;
}
return t;
}
}

public ShoppingCart()
{
if (this._items == null)
{
this._items = new List<CartItem>();
this._dateCreated = DateTime.Now;
}
}

public void Insert(int productID, decimal price, int quantity, string productName)
{
int itemIndex = ItemIndexOfID(productID);
if (itemIndex == -1)
{
CartItem newItem = new CartItem();
newItem.ProductID = productID;
newItem.Quantity = quantity;
newItem.Price = price;
newItem.ProductName = productName;

_items.Add(newItem);
}
else
{
_items[itemIndex].Quantity += quantity;
}
_lastUpdate = DateTime.Now;
}
public void Update(int rowID, int quantity)
{
CartItem item = _items[rowID];
item.Quantity = quantity;
_lastUpdate = DateTime.Now;
}
public void DeleteItem(int rowID)
{
_items.RemoveAt(rowID);
_lastUpdate = DateTime.Now;
}

private int ItemIndexOfID(int productID)
{
int index = 0;
foreach (CartItem item in _items)
{
if (item.ProductID == productID)
{
return index;
}
index += 1;
}
return -1;
}
}
}
接下来,LZ需要用session持久化数据
入添加商品,我们假设从请求页读取到操作addcart
if(Request["topp"]=="addcart")
{

string pdtid=Request["pdtid"];
if(!YTLib.publicOP.IsNumString(pdtid)||pdtid==null)
pdtid="0";
if(pdtid=="0")
{
Response.Write("<script language=\"javascript\">alert(\"You add the product is wrong!\");window.location.href=\"" + pageno + "\"</script>");
return;
}
string quantity=Request["quantity"+pdtid];
if(!YTLib.publicOP.IsNumString(quantity)||quantity==null||quantity.Length==0)
quantity="1";
DataTable dtpdt=siteadmin.Utilities.GetPdtDetailById(int.Parse(pdtid));
int productId=int.Parse(pdtid);
string price=dtpdt.Rows[0]["PdtPrice"].ToString();
string productName=dtpdt.Rows[0]["PdtTitle"].ToString();
if(Session["cart"]==null)
Session["cart"]=new ShoppingCart();
ShoppingCart cart = (ShoppingCart)Session["cart"];
cart.Insert(productId,Convert.ToDecimal(price), int.Parse(quantity), productName);
Response.Write("<script language=\"javascript\">alert(\"Product successfully added!\");window.location.href=\"" + pageno + "\"</script>");
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式