您的位置:首页 > 其它

注册用户实现购物车功能

2006-10-25 11:35 417 查看
代码出自《asp.net2.0开发指南》
很使用的一个功能救放上来了,作为以后的参考吧。
资源管理器里面的相关文件

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<profile>
<properties>
<add name="ShoppingCart" type="ShoppingCart" serializeAs="Binary"/>
</properties>
</profile>
<authorization>
<deny users="?"/>
</authorization>
<authentication mode="Forms">
<forms loginUrl ="Login.aspx"></forms>
</authentication>
<compilation debug="false"/>
</system.web>
</configuration>

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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindShoppingCart();
}
}
// 显示Profile对象中保存的购物车信息
protected void BindShoppingCart()
{
//如果Profile中存储的购物车的商品不为空,则进行数据绑定并计算总价
if (Profile.ShoppingCart != null)
{
CartGrid.DataSource = Profile.ShoppingCart.CartItems;
CartGrid.DataBind();
lblTotal.Text = "总价:" + Profile.ShoppingCart.Total.ToString("c");
}
}
// 将选中商品添加到购物车中
protected void AddCartItem(object sender, EventArgs e)
{
// 获取被选中数据行
GridViewRow row = ProductGrid.SelectedRow;
// 获取主键ID的值
int ID = (int)ProductGrid.SelectedDataKey.Value;
// 获取商品名称
String Name = row.Cells[1].Text;
// 获取商品单价
decimal Price = Decimal.Parse(row.Cells[2].Text, System.Globalization.NumberStyles.Currency);
// 如果Profile中存储的购物车对象为null,则创建一个相应对象
if (Profile.ShoppingCart == null)
{
Profile.ShoppingCart = new ShoppingCart();
}
// 利用前面获取的数据,在Profile对象购物车中添加被选中的商品
Profile.ShoppingCart.AddItem(ID, Name, Price);
// 显示购物车数据
BindShoppingCart();
}
// 将选中商品从购物车中删除
protected void RemoveCartItem(object sender, EventArgs e)
{
// 获取被选中商品的主键ID
int ID = (int)CartGrid.SelectedDataKey.Value;
// 利用ID,从Profile对象购物车中删除该商品
Profile.ShoppingCart.RemoveItem(ID);
// 显示购物车数据
BindShoppingCart();
}
}

Code下载:http://files.cnblogs.com/hide0511/shoppingcart.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: