为注册用户实现复杂的数据类型配置--购物车

来源:互联网 发布:华为网络机顶盒官网 编辑:程序博客网 时间:2024/05/18 03:31

web.config配置

<connectionStrings>
  <add name="NorthwindConnectionString" connectionString="Data Source=b764e28a5fe449f/helen;Initial Catalog=Northwind;Integrated Security=True"
            providerName="System.Data.SqlClient" />
 </connectionStrings>

 

<profile>
   <properties>
    <add name="ShoppingCart" type="ShoppingCart" serializeAs="Binary"/>
   </properties>
  </profile>
  <authorization>
   <deny users="?"/>
  </authorization>
  <authentication mode="Forms">
   <forms loginUrl ="Login.aspx"></forms>
  </authentication>

 

 

ShoppingCart.cs类

using System;
using System.Collections;

[Serializable]
public class ShoppingCart
{
    public Hashtable _CartItems = new Hashtable();
    // 创建属性CartItems,用于获取购物车中所有商品
    public ICollection CartItems
    {
        get { return _CartItems.Values; }
    }

    // 创建属性Total,用于获取购物车中商品总价
    public decimal Total
    {
        get
        {
            decimal sum = 0;
            foreach (CartItem item in _CartItems.Values)
            {
                sum += item.Price * item.Quantity;
            }
            return sum;
        }
    }
    // 实现将商品添加到购物车中
    public void AddItem(int ID, string Name, decimal Price)
    {
        CartItem item = (CartItem)_CartItems[ID];
        if (item == null)
        {
            _CartItems.Add(ID, new CartItem(ID, Name, Price));
        }
        else
        {
            item.Quantity++;
            _CartItems[ID] = item;
        }
    }
    // 从购物车中删除商品
    public void RemoveItem(int ID)
    {
        CartItem item = (CartItem)_CartItems[ID];
        if (item == null)
        {
            return;
        }
        item.Quantity--;
        if (item.Quantity == 0)
        {
            _CartItems.Remove(ID);
        }
        else
        {
            _CartItems[ID] = item;
        }
    }
}

//实现表示商品的业务实体类
[Serializable]
public class CartItem
{
    private int _ID;
    private string _Name;
    private decimal _Price;
    private int _Quantity = 1;
    // 创建属性ID
    public int ID
    {
        get { return _ID; }
    }
    // 创建属性Name
    public string Name
    {
        get { return _Name; }
    }
    // 创建属性Price
    public decimal Price
    {
        get { return _Price; }
    }
    // 创建属性Quantity
    public int Quantity
    {
        get { return _Quantity; }
        set { _Quantity = value; }
    }
    // 创建类构造函数
    public CartItem(int ID, string Name, decimal Price)
    {
        _ID = ID;
        _Name = Name;
        _Price = Price;
    }
}

 

 

 

default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>为注册用户实现复杂数据类型的个性化用户配置</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width: 620px">
                <legend class="mainTitle">实现验证用户个性化用户配置</legend>
                <table border="0" cellpadding="5" width="620px">
                    <tr align="center">
                        <td>
                            <p class="littleTitle">
                                待售商品列表</p>
                        </td>
                        <td>
                            <p class="littleTitle">
                                购物车</p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                                SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]"></asp:SqlDataSource>
                            <asp:GridView ID="ProductGrid" runat="server" OnSelectedIndexChanged="AddCartItem"
                                Width="300px" AllowPaging="True" AutoGenerateColumns="False" BackColor="White"
                                BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ProductID"
                                DataSourceID="SqlDataSource1" Font-Size="Small" PageSize="5">
                                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                                <RowStyle BackColor="White" ForeColor="#330099" />
                                <Columns>
                                    <asp:CommandField ButtonType="Image" SelectImageUrl="~/Images/button_buy.gif" ShowSelectButton="True" />
                                    <asp:BoundField DataField="ProductName" HeaderText="商品名称" SortExpression="ProductName" />
                                    <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="单价" SortExpression="UnitPrice" />
                                </Columns>
                                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                            </asp:GridView>
                        </td>
                        <td valign="top">
                            <asp:GridView ID="CartGrid" AutoGenerateColumns="False" DataKeyNames="ID" OnSelectedIndexChanged="RemoveCartItem"
                                CellPadding="4" Width="320px" runat="Server" BackColor="White" BorderColor="#CC9966"
                                BorderStyle="None" BorderWidth="1px" Font-Size="Small">
                                <Columns>
                                    <asp:ButtonField CommandName="select" Text="Remove" ButtonType="Image" ImageUrl="~/Images/button_del.gif" />
                                    <asp:BoundField DataField="Name" HeaderText="商品名称" />
                                    <asp:BoundField DataField="Price" HeaderText="单价" DataFormatString="{0:c}" />
                                    <asp:BoundField DataField="Quantity" HeaderText="数量" />
                                </Columns>
                                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                                <RowStyle BackColor="White" ForeColor="#330099" />
                                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                            </asp:GridView>
                            <asp:Label ID="lblTotal" runat="Server" CssClass="littleTitle" /></td>
                    </tr>
                </table>
            </fieldset>
        </div>
    </form>
</body>
</html>

 

.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();
    }
}

原创粉丝点击