日期:2010-01-21  浏览次数:20431 次

该购物车的功能如下:
. 通过ajax实现添加和删除车上的物品。
. 删除的物品会显示出来,可以重新添加到购物车。
. 嗯...没有了,具体大家接着看吧。

购物车的结构我打算用一个table来展示,在UserControl里使用ListView展现购物车的物品(因为比拼接字符串要容易维护的多)。具体代码如下(ShopCartTest.ascx):
复制代码 代码如下:

<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table runat="server" cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td width='7%' style='height: 30px'>
商品编号
</td>
<td>
商品名称
</td>
<td width='10%'>
京东价
</td>
<td width='8%'>
返现
</td>
<td width='8%'>
赠送积分
</td>
<td width='9%'>
商品数量
</td>
<td width='7%'>
删除商品
</td>
</tr>
<tr runat="server" id="itemPlaceholder" />
<tr>
<td colspan='7' style='height: 30px'>
重量总计:<%= this.GetProductsWeight() %>kg 原始金额:¥307.00元 - 返现:¥0.00元<br />
<span style='font-size: 14px'><b>商品总金额(不含运费):<span id='cartBottom_price'>¥307.00</span>元</b></span>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td style='padding: 5px 0 5px 0;'>
<%#(Container.DataItem as Product).ID %>
</td>
<td>
<a target='_blank' href='http://www.xxx.com/product/<%#(Container.DataItem as Product).ID %>.html'>
<%#(Container.DataItem as Product).Name %></a>
</td>
<td>
<span>
<%#(Container.DataItem as Product).Price %></span>
</td>
<td>
<%#(Container.DataItem as Product).BackMoney %>
</td>
<td>
<%#(Container.DataItem as Product).Score %>
</td>
<td>
<a href='#none' title='减一'
style='text-decoration: none'>-</a><input type='text' id='txt<%#(Container.DataItem as Product).ID %>'
name='txtChange<%#(Container.DataItem as Product).ID %>' maxlength='4' style='width: 30px'
value='<%#(Container.DataItem as Product).Count %>' /><a href='#none' title='加一'
style='text-decoration: none'>+</a>
</td>
<td>
<a href='#none' id='btn_del_173259' >
删除</a>
</td>
</tr>
</ItemTemplate>
</asp:ListView>

我想大家应不用我解释代码的意思了,很简单。
后台代码如下:
复制代码 代码如下:

public partial class ShopCartTest : System.Web.UI.UserControl
{
List<Product> productsList = null;
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
switch (Acion)
{
case "removeProductOnShoppingCart":
productsList = Product.GetProductsInCart(ProductID);
break;
case "changeProductCount":
productsList = Product.GetProductsInCart(null);
foreach (var item in productsList)
{
if (item.ID == ProductID)
{
item.Count = "3";
}
}
break;
case "AddProduct":
productsList = Product.GetProductsInCart(null);
productsList.Add(new Product() { ID = "173233", Name = "ElandMX9470", Price = "399.00", BackMoney = "0.00", Score = "0", Count = "1" });
break;
default:
productsList = Product.GetProductsInCart(ProductID);
break;
}
ListView1.DataSource = productsList;
ListView1.DataBind();
}
public string GetProductsWeight()
{
return Product.GetProductsInCart(ProductID).Sum(p => decimal.Parse(p.Price) * decimal.Parse(p.Count)).ToString();
}
public string GetProductsOrigin