[ASP.NET开发系列] 购物推车程序开发——调用购物车类
[ASP.NET开发系列] 购物推车程序开发——调用购物车类 [ASP.NET开发系列] 购物推车程序开发——调用购物车类
上一篇《购物推车程序开发——调用购物车类》文件定义和部署了一个购物车类(ShoppingCart.cs),其中AddItem()和DeleteItem()两个方法分别完成了商品的添加和删除功能。这一篇文章着重如何在ASPX文件里调用该购物车类,使用C#语言编写。
Default.aspx
<%@ Import Namespace='WendwCart'%>
<%@ Import Namespace='System.Data'%>
<%@ Import Namespace='System.Data.SqlClient'%>
<html>
<body>
<form runat='server'>
<table width='100%'>
<tr>
<td><asp:Button Text='查看' OnClick=' View_ShoppingCart ' runat='server'/></td>
</tr>
</table>
<br>
<center>
<asp:DataGrid ID='MyDataGrid'
AutoGenerateColumns='false' Cellpadding='2'
BorderWidth='1' BorderColor='lightgray'
Font-Name='Verdana' Font-Size='10pt'
GridLines='vertical' Width='90%'
OnItemCommand='OnItemCommand' runat='server'>
<columns>
<asp:BoundColumn HeaderText='商品ID' DataField='ItemID'/>
<asp:BoundColumn headerText='名 称' DataField=' ShangpinName '
headerStyle-HorizontalAlign='center'/>
<asp:BoundColumn HeaderText='价 格' DataField='Price'
DataFormatString='{0:c}' headerStyle-HorizontalAlign='center'
ItemStyle-HorizontalAlign='right'/>
<asp:ButtonColumn HeaderText='订 购' Text='加入购物车'
headerStyle-HorizontalAlign='center'
ItemStyle-HorizontalAlign='center' CommandName='AddToCart'/>
</columns>
<HeaderStyle BackColor='teal' ForeColor='white' Font-Bold='true'/>
<ItemStyle BackColor='white' ForeColor='darkblue'/>
<AlternatingItemStyle BackColor='beige' ForeColor='darkblue'/>
</asp:DataGrid>
</center>
</form>
</body>
</html>
//连接数据库绑定数据到DataGrid控件
<script Language='C#' runat='server'>
void Page_Load(Object sender,EventArgs e){
if(!IsPostBack){
String ConnectString=ConfigurationSettings.AppSettings['DataConnectionString'];
SqlDataAdapter adapter=new SqlDataAdapter('select * from Tb_ShangPin where sp_Price!=0',ConnectString);
DataSet ds=new DataSet();
adapter.Fill(ds);
MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();
}
}
//添加商品
void OnItemCommand(Object sender,DataGridCommandEventArgs e){
if(e.CommandName=='AddToCart'){
Stat_Class order=new Stat_Class (e.Item.Cells[0].Text,e.Item.Cells[1].Text,Convert.ToDecimal(e.Item.Cells[2].Text.Substring(1)),1);
ShoppingCart cart=(ShoppingCart)Session['MyShoppingCart']; //创建实例
if(cart!=null)
cart.AddItem(order);
}
}
//查看购物车,通过Response.Redirect()转到View_ShoppingCart.aspx页面
void View_ShoppingCart(Object sender,EventArgs e)
{
Response.Redirect('View_ShoppingCart.aspx');
}
</script>
说明:本例使用到数据库,在运行程序之前,我们先创建一个数据库和表,分别取名为Test_Db和Tb_ShangPin。表Tb_ShangPin有很多字段,其中包含sp_id、sp_Name、sp_Price几个字段。接下来我们把连接数据库的字符串写进web.config文件里,代码如下:
<configuration>
<appSettings>
<add key='DataConnectionString' value='server=(local);database=Test_Db;Trusted_Connection=no;uid=sa;pwd=1234'/>
</appSettings>
</configuration>
查看购物车程序View_ShoppingCart.aspx
<%@ Import Namespace='WendwCart'%>
<html>
<body>
<form runat='server'>
<table width='100%'>
<tr>
<td>
<asp:Button Text='继续购物' OnClick='Gonoshopping' runat='server'/>
</td>
</tr>
</table>
<br>
<center>
<asp:DataGrid ID='MyDataGrid'
AutoGenerateColumns='false'
Cellpadding='2'
BorderWidth='1'
BorderColor='lightgray'
Font-Name='Verdana'
Font-Size='10pt'
GridLines='Vertical'
Width='90%'
OnItemCommand='OnItemCommand'
runat='server'>
<columns>
<asp:BoundColumn HeaderText='图书ID' DataField='ItemID'/>
<asp:BoundColumn headerText='书 名' DataField=' ShangpinName '/>
<asp:BoundColumn HeaderText='价 格' DataField='Price'
DataFormatString='{0:c}' headerStyle-HorizontalAlign='center'
ItemStyle-HorizontalAlign='right'/>
<asp:BoundColumn HeaderText='数量' DataField='Quantity'
headerStyle-HorizontalAlign='center'
ItemStyle-HorizontalAlign='center'/>
<asp:ButtonColumn HeaderText='删除' Text='删除'
headerStyle-HorizontalAlign='center'
ItemStyle-HorizontalAlign='center' CommandName='DelFromCart'/>
</columns>
<HeaderStyle BackColor='teal' ForeColor='white' Font-Bold='true'/>
<ItemStyle BackColor='white' ForeColor='darkblue'/>
<AlternatingItemStyle BackColor='beige' ForeColor='darkblue'/>
</asp:DataGrid>
</center>
<h3><asp:Label ID='Total' runat='server'/></h3>
</form>
</body>
</html>
<script Language='C#' runat='server'>
void Page_Load(Object sender,EventArgs e){
ShoppingCart cart=(ShoppingCart)Session['MyShoppingCart']; //创建实例
if(cart !=null){
MyDataGrid.DataSource=cart.Orders;
MyDataGrid.DataBind();
Total.Text=String.Format('合计:{0:c}',cart.TotalCost);
}
}
void OnItemCommand(Object sender,DataGridCommandEventArgs e){
if(e.CommandName=='DelFromCart'){
ShoppingCart cart=(ShoppingCart)Session['MyShoppingCart'];
if(cart != null){
cart.DeleteItem(e.Item.Cells[0].Text);
MyDataGrid.DataBind();
Total.Text=String.Format('合计:{0:c}',cart.TotalCost);
}
}
}
public void Gonoshopping(Object sender,EventArgs e)
{
Response.Redirect('default.aspx');
}
</script>
总结:这两篇文章只是简单的介绍一下购物推车的基本原理,要应用到实际项目中还需要做很多工作。