注册用户实现购物车功能[转载]
代码出自《asp.net2.0开发指南》
很使用的一个功能救放上来了,作为以后的参考吧。
资源管理器里面的相关文件
App_Code里面有一个类文件ShopingCart,使用哈希表来存储购买商品的实体
Default页里面包含2个GridView控件。
类的关系
Web.config
<?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>
<?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>
default.aspx.cs
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();
}
}
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下载:https://files.cnblogs.com/hide0511/shoppingcart.rar
本博客内容,如需转载请务必保留超链接。Contact Me:Mail此处省略好几个字...