注册用户实现购物车功能

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


App_Code里面有一个类文件ShopingCart,使用哈希表来存储购买商品的实体
Default页里面包含2个GridView控件。









    类的关系
      

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

ContractedBlock.gif ExpandedBlockStart.gif default.aspx.cs
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
None.gif
public partial class _Default : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            BindShoppingCart();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 显示Profile对象中保存的购物车信息
InBlock.gif
    protected void BindShoppingCart()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//如果Profile中存储的购物车的商品不为空,则进行数据绑定并计算总价
InBlock.gif
        if (Profile.ShoppingCart != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CartGrid.DataSource 
= Profile.ShoppingCart.CartItems;
InBlock.gif            CartGrid.DataBind();
InBlock.gif            lblTotal.Text 
= "总价:" + Profile.ShoppingCart.Total.ToString("c");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 将选中商品添加到购物车中
InBlock.gif
    protected void AddCartItem(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 获取被选中数据行
InBlock.gif
        GridViewRow row = ProductGrid.SelectedRow;
InBlock.gif        
// 获取主键ID的值
InBlock.gif
        int ID = (int)ProductGrid.SelectedDataKey.Value;
InBlock.gif        
// 获取商品名称
InBlock.gif
        String Name = row.Cells[1].Text;
InBlock.gif        
// 获取商品单价
InBlock.gif
        decimal Price = Decimal.Parse(row.Cells[2].Text, System.Globalization.NumberStyles.Currency);
InBlock.gif        
// 如果Profile中存储的购物车对象为null,则创建一个相应对象
InBlock.gif
        if (Profile.ShoppingCart == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Profile.ShoppingCart 
= new ShoppingCart();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// 利用前面获取的数据,在Profile对象购物车中添加被选中的商品
InBlock.gif
        Profile.ShoppingCart.AddItem(ID, Name, Price);
InBlock.gif        
// 显示购物车数据
InBlock.gif
        BindShoppingCart();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// 将选中商品从购物车中删除
InBlock.gif
    protected void RemoveCartItem(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 获取被选中商品的主键ID
InBlock.gif
        int ID = (int)CartGrid.SelectedDataKey.Value;
InBlock.gif        
// 利用ID,从Profile对象购物车中删除该商品
InBlock.gif
        Profile.ShoppingCart.RemoveItem(ID);
InBlock.gif        
// 显示购物车数据
InBlock.gif
        BindShoppingCart();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

Code下载: http://files.cnblogs.com/hide0511/shoppingcart.rar

转载于:https://www.cnblogs.com/hide0511/archive/2006/10/25/539268.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值