本示例利用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=
//一種書的總金額
public decimal Sum
{
get{return sum;}
set{sum=value;}
}
}
}
//購物車集合
//Books 用戶所有訂購的書
using System;
using System
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
{
((Book)ht[b
}
else
{
ht
}
}
public void Remove(string bookid)
{
if(ht
ht
}
//統計有多少種書
public int Count
{
get
{
return ht
}
}
public void Clear()
{
ht
}
public Book this[string bookid]
{
get
{
if(ht
return (Book)ht[bookid];
return null;
}
}
#region IEnumerable 成員
public IEnumerator GetEnumerator()
{
// TODO: 添加 Books
return ht
}
#endregion
}
}
//此頁面主要是用於顯示所有的書
//此頁面中每本書都要顯示封面
<%@ Page language=
<!DOCTYPE HTML PUBLIC
<HTML>
<HEAD>
<title>BookList</title>
<meta content=
<meta content=
<meta content=
<meta content=
<LINK text/css
</HEAD>
<body MS_POSITIONING=
<form id=
<asp:datalist id=
DataKeyField=
<ItemTemplate>
<TABLE id=
<TR>
<TD>
<a <%#
<!
</asp:Image>
</a>
</TD>
<TD vAlign=
<TABLE id=
<TR>
<TD>書名
<asp:Label id=Label
</asp:Label></TD>
</TR>
<TR>
<TD>圖書簡介
<asp:Label id=Label
</asp:Label></TD>
</TR>
<TR>
<TD>金額
<asp:Label id=Label
</asp:Label></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<asp:Label id=
<asp:Label id=Label
</asp:Label></TD>
<TD align=
<asp:ImageButton id=
</TR>
</TABLE>
</ItemTemplate>
<AlternatingItemTemplate>
<TABLE id=
<TR>
<TD>
<a <%#
<!
</asp:Image></a></TD>
<TD vAlign=
<TABLE id=
<TR>
<TD>書名
<asp:Label id=Label
</asp:Label></TD>
</TR>
<TR>
<TD>圖書簡介
<asp:Label id=Label
</asp:Label></TD>
</TR>
<TR>
<TD>金額
<asp:Label id=Label
</asp:Label></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<asp:Label id=
<asp:Label id=Label
</asp:Label></TD>
<TD align=
<asp:ImageButton id=
</TR>
</TABLE>
</AlternatingItemTemplate>
</asp:datalist></form>
</body>
</HTML>
//CS CODE
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
namespace CartTest
{
/// <summary>
/// BookList 的摘要說明
/// </summary>
public class BookList : System
{
protected System
private void Page_Load(object sender
{
if(!this
{
SqlConnection cn=new SqlConnection();
cn
cn
SqlCommand cmd=new SqlCommand();
cmd
cmd
SqlDataAdapter da=new SqlDataAdapter();
da
DataSet ds=new DataSet();
da
cn
this
this
}
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP
//
InitializeComponent();
base
}
/// <summary>
/// 設計器支持所需的方法
/// 此方法的內容
/// </summary>
private void InitializeComponent()
{
this
this
}
#endregion
private void DataList
{
//用戶選中一本書後
string bookGuid=this
Book b=new Book();
//首先獲得自己的購物車
Books bs=(Books)Session[
b
b
//根據ITEM的類型取值
if(e
{
b
b
}
else if(e
{
b
b
}
//將書加入到購物車
bs
Session[
//打開購物車頁面
Response
}
}
}
//圖片處理頁
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
namespace CartTest
{
/// <summary>
/// ImageView 的摘要說明
/// </summary>
public class ImageView : System
{
private void Page_Load(object sender
{
SqlConnection cn=new SqlConnection();
cn
cn
SqlCommand cmd=new SqlCommand();
cmd
cmd
//cmd
SqlDataAdapter da=new SqlDataAdapter();
da
DataSet ds=new DataSet();
da
cn
Response
Response
Response
Response
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP
//
InitializeComponent();
base
}
/// <summary>
/// 設計器支持所需的方法
/// 此方法的內容
/// </summary>
private void InitializeComponent()
{
this
}
#endregion
}
}
//當用戶選取其中一本書時
<%@ Page language=
<!DOCTYPE HTML PUBLIC
<HTML>
<HEAD>
<title>BookView</title>
<meta name=
<meta name=
<meta name=
<meta name=
</HEAD>
<body MS_POSITIONING=
<form id=
<FONT face=
<asp:Label id=
Width=
<asp:Image id=
Width=
<asp:Label id=
Width=
<asp:Label id=
<asp:Panel id=Panel
<asp:Label id=Label
</form>
</body>
</HTML>
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
namespace CartTest
{
/// <summary>
/// BookView 的摘要說明
/// </summary>
public class BookView : System
{
protected System
protected System
protected System
protected System
protected System
protected System
protected System
protected System
protected System
private void Page_Load(object sender
{
if(!this
{
if(this
{
this
SqlConnection cn=new SqlConnection();
cn
cn
SqlCommand cmd=new SqlCommand();
cmd
cmd
//cmd
SqlDataAdapter da=new SqlDataAdapter();
da
DataSet ds=new DataSet();
da
cn
this
this
this
this
}
}
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP
//
InitializeComponent();
base
}
/// <summary>
/// 設計器支持所需的方法
/// 此方法的內容
/// </summary>
private void InitializeComponent()
{
this
}
#endregion
}
}
//購物車頁面
<%@ Page language=
<!DOCTYPE HTML PUBLIC
<HTML>
<HEAD>
<title>WebForm
<meta name=
<meta name=
<meta name=
<meta name=
<script>
//此JS主要是防止用戶輸入非數字
function checkNum()
{
var chr=String
if(isNaN(chr))
{
event
}
}
</script>
</HEAD>
<body MS_POSITIONING=
<form id=
<asp:DataGrid id=
CellPadding=
ShowFooter=
<ItemStyle BackColor=
<HeaderStyle Font
<Columns>
<asp:TemplateColumn HeaderText=
<ItemTemplate>
<asp:Label id=Label
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=
<ItemTemplate>
<asp:TextBox id=txtPrice runat=
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=
<ItemTemplate>
<asp:TextBox id=txtNum onkeypress=
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=
<ItemTemplate>
<asp:TextBox id=txtSum runat=
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id=
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=
<ItemTemplate>
<asp:LinkButton id=
<asp:LinkButton id=
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle NextPageText=
</asp:DataGrid>
</form>
</body>
</HTML>
//購物車察看頁裡的數據是Session裡所存放的Books集合對象
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
namespace CartTest
{
/// <summary>
/// WebForm
/// </summary>
public class WebForm
{
protected System
private void Page_Load(object sender
{
if(!this
{
Books bs=(Books)Session[
this
this
}
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP
//
InitializeComponent();
base
}
/// <summary>
/// 設計器支持所需的方法
/// 此方法的內容
/// </summary>
private void InitializeComponent()
{
this
this
this
this
}
#endregion
//利用此事件對網格控件的外觀進行控件(合並列)
private void DataGrid
{
ListItemType itemType = e
if (itemType == ListItemType
{
// e
// e
e
e
e
e
}
}
private void DataGrid
{
Books bs=(Books)Session[
if(e
{
int num=Convert
decimal p=Convert
bs[this
bs[this
}
else if(e
{
bs
}
this
this
Session[
}
private void DataGrid
{
ListItemType itemType = e
if (itemType == ListItemType
{
decimal sum=
foreach(DataGridItem item in this
{
decimal p=Convert
int n=Convert
sum+=p*n;
}
((TextBox)e
}
}
}
}
此外我們還要在Global
此購物車實現的原理很簡單
From:http://tw.wingwit.com/Article/program/net/201311/11934.html