购物车的C实现及结算处理

2020-07-02 淮安装修公司

本示例利用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 llections;

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(ntainsKey(okID))

{

((Book)ht[okID]).Num=((Book)ht[okID]).Num+m;

}

else

{

d(okID,b);

}

}

public void Remove(string bookid)

{

if(ntainsKey(bookid))

move(bookid);

}

//统计有多少种书

public int Count

{

get

{

return unt;

}

}

public void Clear()

{

ear();

}

public Book this[string bookid]

{

get

{

if(ntainsKey(bookid))

return (Book)ht[bookid];

return null;

}

}

#region IEnumerable 成员

public IEnumerator GetEnumerator()

{

// TODO: 添加 tEnumerator 实现

return tEnumerator();

}

#endregion

}

}

//此页面主要是用于显示所有的书。用的是DataList来自定义显示模板。但是实际上可以使用DataGrid来处理。DataGrid也可以实现分页功能及自定义模板。只要将dDatagrid设为一个模板列,然后将DataList里的模板列代码Copy过去即可。

//此页面中每本书都要显示封面。这个问题我们可以通过一个过渡页来处理图片数据

%@ Page language="c#" Codebehind="" AutoEventWireup="false" Inherits="okList" %

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

HTML

HEAD

titleBookList/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="" name="vs_targetSchema"

LINK href="http://localhost/CartTest/s" 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='%# "px?BookID="+al(Container, "okGuid") %'

!px页面专用来处理书的图片--asp:Image id=Image1 runat="server" Width="120px" Height="144px" ImageUrl='%# "px?imgid="+al(Container, "okGuid") %'

/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='%# al(Container, "okTitle") %'

/asp:Label/TD

/TR

TR

TD图书简介:

asp:Label id=Label2 style="OVERFLOW: hidden; TEXT-OVERFLOW: ellipsis" runat="server" Width="496" Text='%# "nobr"+al(Container, "okComment")+"/nobr"%' Height="50px"

/asp:Label/TD

/TR

TR

TD金额:

asp:Label id=Label3 runat="server" Text='%# al(Container, "ice","{0:C}") %'

/asp:Label/TD

/TR

/TABLE

/TD

/TR

TR

TD

asp:Label id="Label4" runat="server"日期:/asp:Label

asp:Label id=Label5 runat="server" Text='%# al(Container, "blishDate", "{0:D}") %'

/asp:Label/TD

TD align="right"

asp:ImageButton id="Imagebutton1" runat="server" ImageUrl="f" CommandName="AddCart"/asp:ImageButton/TD

/TR

/TABLE

/ItemTemplate

AlternatingItemTemplate

TABLE id="Table4" cellSpacing="1" cellPadding="1" bgColor="#eefeff" border="0"

TR

TD

a href='%# "px?BookID="+al(Container, "okGuid") %'

!px页面专用来处理书的图片--asp:Image id=Image2 runat="server" Width="120px" Height="144px" ImageUrl='%# "px?imgid="+al(Container, "okGuid") %'

/asp:Image/a/TD

TD vAlign="top"

TABLE id="Table5" cellSpacing="1" cellPadding="1" width="300" border="1"

TR

TD书名:

asp:Label id=Label6 runat="server" Text='%# al(Container, "okTitle") %'

/asp:Label/TD

/TR

TR

TD图书简介:

asp:Label id=Label7 style="OVERFLOW: hidden; TEXT-OVERFLOW: ellipsis" runat="server" Width="496px" Text='%# al(Container, "okComment") %' Height="50px"

/asp:Label/TD

/TR

TR

TD金额:

asp:Label id=Label8 runat="server" Text='%# al(Container, "ice") %'

/asp:Label/TD

/TR

/TABLE

/TD

/TR

TR

TD

asp:Label id="Label9" runat="server"日期:/asp:Label

asp:Label id=Label10 runat="server" Text='%# al(Container, "blishDate") %'

/asp:Label/TD

TD align="right"

asp:ImageButton id="Imagebutton2" runat="server" ImageUrl="f"/asp:ImageButton/TD

/TR

/TABLE

/AlternatingItemTemplate

/asp:datalist/form

/body

/HTML

//CS CODE

using System;

using llections;

using ponentModel;

using ta;

using awing;

using b;

using ssionState;

using ;

using bControls;

using mlControls;

using lClient;

namespace CartTest

{

/// summary

/// BookList 的摘要说明。

/// /summary

public class BookList : ge

{

protected taList DataList1;

private void Page_Load(object sender, entArgs e)

{

if(!PostBack)

{

SqlConnection cn=new SqlConnection();

nnectionString="server=.;uid=sa;pwd=;database=p1";

en();

SqlCommand cmd=new SqlCommand();

nnection=cn;

mandText="select * from books ";

SqlDataAdapter da=new SqlDataAdapter();

lectCommand=cmd;

DataSet ds=new DataSet();

ll(ds);

ose();

taSource=bles[0];

taBind();

}

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 Web 窗体设计器所必需的。

//

InitializeComponent();

Init(e);

}

/// summary

/// 设计器支持所需的方法 - 不要使用代码器修改

/// 此方法的内容。

/// /summary

private void InitializeComponent()

{

emCommand += new taListCommandEventHandler(taList1_ItemCommand);

ad += new entHandler(ge_Load);

}

#endregion

private void DataList1_ItemCommand(object source, taListCommandEventArgs e)

{

//用户选中一本书后,默认订一本书

string bookGuid=taKeys[emIndex].ToString();

Book b=new Book();

//首先获得自己的购物车

Books bs=(Books)Session["MyCart"];

okID=bookGuid;

m=1;

//根据ITEM的类型取值

if(emType==em)

{

ice=Decimal(((Label)ndControl("Label3")).bstring(1));

tle=((Label)ndControl("Label1")).Text;

}

else if(emType==ternatingItem)

{

ice=Decimal(((Label)ndControl("Label8")).bstring(1));

tle=((Label)ndControl("Label6")).Text;

}

//将书加入到购物车

d(b);

Session["MyCart"]=bs;

//打开购物车页面。

ite("en('px')/script");

}

}

}

//图片处理页

using System;

using llections;

using ponentModel;

using ta;

using awing;

using b;

using ssionState;

using ;

using bControls;

using mlControls;

using lClient;

namespace CartTest

{

/// summary

/// ImageView 的摘要说明。

/// /summary

public class ImageView : ge

{

private void Page_Load(object sender人民西安8月20日电(庞铭)“我也想逃脱, entArgs e)

{

SqlConnection cn=new SqlConnection();

nnectionString="server=.;uid=sa;pwd=;database=p1";

en();

SqlCommand cmd=new SqlCommand();

nnection=cn;

mandText="select cover from books where bookguid='"+ eryString["imgid"].ToString() +"'";

//mandText="select cover from books where bookguid='350bc228-a12d-4c15-b8ee625e40403e'";

SqlDataAdapter da=new SqlDataAdapter();

lectCommand=cmd;

DataSet ds=new DataSet();

ll(ds);

ose();

ear();

earContent();

ntentType="Image/jpg";

naryWrite((byte[])bles[0]公务员提薪、农民收入回升有利于扩大消费需求。 综合投资、消费和出口三大需求因素来看.Rows[0][0]);

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 Web 窗体设计器所必需的。

//

InitializeComponent();

Init(e);

}

/// summary

/// 设计器支持所需的方法 - 不要使用代码器修改

/// 此方法的内容。

/// /summary

private void InitializeComponent()

{

ad += new entHandler(ge_Load);

}

#endregion

}

}

查看本文来源

灰指甲脚气偏方
宣城白斑疯医院
剖宫产术后怎么防止腹胀便秘
早泄
男性不育
性功能障碍
为你推荐