本示例利用Session对象来实现一个简单的购物车。主要用于教学演示。
Book类
此类主是代表购物车的一本书
using System;
namespace CartTest
{
/// <summary>
/// Books 的摘要说明。
/// </summary>
public class Book
{
string bookid;
string title;
decimal price;
int num;
public Book()
{
}
/// <summary>
/// ID
/// </summary>
public string BookID
{
get{return bookid;}
set{bookid=value;}
}
/// <summary>
/// 书名
/// </summary>
public string Title
{
get{return title;}
set{title=value;}
}
/// <summary>
/// 金额
/// </summary>
public decimal Price
{
get{return price;}
set{price=value;
sum=price*num;
}
}
/// <summary>
/// 数量
/// </summary>
public int Num
{
get{return num;}
set{num=value;
sum=price*num;
}
}
decimal sum=0m;
//一种书的总金额
public decimal Sum
{
get{return sum;}
set{sum=value;}
}
}
}
//购物车集合
//Books 用户所有订购的书 ,实现IEnumerable接口,我们可以将其绑定到datagrid控件
using System;
using System.Collections;
namespace CartTest
{
/// <summary>
///
/// </summary>
public class Books :IEnumerable
{
Hashtable ht=null;
public Books()
{
ht=new Hashtable();
}
public Books(int count)
{
ht=new Hashtable(count);
}
public void Add(Book b)
{
//如果集合中有相同ID的书,则对书的数量进行相加
if(ht.ContainsKey(b.BookID))
{
((Book)ht[b.BookID]).Num=((Book)ht[b.BookID]).Num+b.Num;
}
else
{
ht.Add(b.BookID,b);
}
}
public void Remove(string bookid)
{
if(ht.ContainsKey(bookid))
ht.Remove(bookid);
}
//统计有多少种书
public int Count
{
get
{
return ht.Count;
}
}
public void Clear()
{
ht.Clear();
}
public Book this[string bookid]
{
get
{
if(ht.ContainsKey(bookid))
return (Book)ht[bookid];
return null;
}
}
#region IEnumerable 成员
public IEnumerator GetEnumerator()
{
// TODO: 添加 Books.GetEnumerator 实现
return ht.Values.GetEnumerator();
}
#endregion
}
}
//此页面主要是用于显示所有的书。用的是DataList来自定义显示模板。但是实际上可以使用DataGrid来处理。DataGrid也可以实现分页功能及自定义模板。只要将dDatagrid设为一个模板列,然后将DataList里的模板列代码Copy过去即可。
//此页面中每本书都要显示封面。这个问题我们可以通过一个过渡页来处理图片数据
<%@ Page language="c#" Codebehind="BookList.aspx.cs" AutoEventWireup="false" Inherits="CartTest.BookList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>BookList</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="javascript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="http://localhost/CartTest/StyleSheet1.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="DataList1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server"
DataKeyField="BookGuid" Width="650">
<ItemTemplate>
<TABLE id="Table14" cellSpacing="1" cellPadding="1" border="0">
<TR>
<TD>
<a href='<%# "BookView.aspx?BookID="+DataBinder.Eval(Container, "DataItem.BookGuid") %>'>
<!--imageview.aspx页面专用来处理书的图片--> <asp:Image id=Image1 runat="server" Width="120px" Height="144px" ImageUrl='<%# "ImageView.aspx?imgid="+DataBinder.Eval(Container, "DataItem.BookGuid") %>'>
</asp:Image>
</a>
</TD>
<TD vAlign="top">
<TABLE id="Table15" cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD>书名:
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.BookTitle") %>'>
</asp:Label></TD>
</TR>
<TR>
<TD>图书简介:
<asp:Label id=Label2 style="OVERFLOW: hidden; TEXT-OVERFLOW: ellipsis" runat="server" Width="496" Text='<%# "<nobr>"+DataBinder.Eval(Container, "DataItem.BookComment")+"/<nobr>"%>' Height="50px">
</asp:Label></TD>
</TR>
<TR>
<TD>金额:
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Price","{0:C}") %>'>
</asp:Label></TD>
</TR>
&nbs