注册用户实现购物车功能
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
很使用的一个功能救放上来了,作为以后的参考吧。
资源管理器里面的相关文件
<?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
相关文章推荐
- 注册功能实现后台数据库添加用户信息
- ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
- Html5实现用户注册自动校验功能实例代码
- Python通过Django实现用户注册和邮箱验证功能代码
- PHP实现的激活用户注册验证邮箱功能示例
- JS实现用户注册时获取短信验证码和倒计时功能
- jQuery ajax MD5实现用户注册即时验证功能
- maven+springMVC+jdbc实现用户注册功能
- 实现用户注册功能:用servlet验证提交的数据是否合法,不合法返回注册页面,并提示出错。
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(五)——实现注册功能
- 如何实现用户注册时记住账号和密码的功能?
- JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
- Node.js+Express+MySql实现用户登录注册功能
- C语言怎么将用户账号密码写入文件实现登录注册功能?
- 用MVC模式实现简单用户登录注册功能
- JSP模式2 实现用户注册的功能
- PHP实现的注册,登录及查询用户资料功能API接口示例
- ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
- Android之SqliteDatabase(MVP模式)实现用户登录注册功能
- web用户控件实现注册功能+前台验证