<%@ Application Language="C#" %>
<%@ Import Namespace="WendwCart" %>
<script runat="server">

    
void Application_Start(object sender, EventArgs e) 
    
{
        
// 在应用程序启动时运行的代码
      

    }

    
    
void Application_End(object sender, EventArgs e) 
    
{
        
//  在应用程序关闭时运行的代码

    }

        
    
void Application_Error(object sender, EventArgs e) 
    

        
// 在出现未处理的错误时运行的代码

    }


    
void Session_Start(object sender, EventArgs e) 
    
{
        
// 在新会话启动时运行的代码
        Session["MyShoppingCart"= new ShoppingCart();
    }


    
void Session_End(object sender, EventArgs e) 
    
{
        
// 在会话结束时运行的代码。 
        
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        
// 或 SQLServer,则不会引发该事件。

    }

       
</script>

ShoppingCart.cs类代码:

using System.Collections;
/// <summary>
/// ShoppingCart 的摘要说明
/// </summary>

namespace WendwCart     //命名空间名称
{
    [Serializable]
    
//定义商品类,保存商品的各种属性
    public class Stat_Class
    

       
private String ShangPinID; //商品ID
      private String Sp_Name; //商品名称
     private decimal Sp_Price; //商品价格
     private int Sp_Quan; //商品数量
       
        
public string ItemID
        
{
            
get return ShangPinID; }
            
//set { ShangPinID = value; }
        }


        
public string ShangpinName
        
{
            
get return Sp_Name; }
            
//set { Sp_Name = value; }
        }


        
public decimal Price
        
{
            
get return Sp_Price; }
            
//set { Sp_Price = value; }
        }


        
public int Quantity
        
{
            
get return Sp_Quan; }
            
set { Sp_Quan = value; }
        }

       
//构造方法,初始化商品的各个属性
        public Stat_Class(string ItemID, string ShangpinName, decimal Price, int Quantity)
        
{
            ShangPinID 
= ItemID;
            Sp_Name 
= ShangpinName;
            Sp_Price 
= Price;
            Sp_Quan 
= Quantity;
        }

    }



    [Serializable]
    
public class ShoppingCart
    
{
        
public ShoppingCart()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        Hashtable Cart_Orders 
= new Hashtable();
        
public ICollection Orders
        
{
            
get return Cart_Orders.Values; }
        }

        
        
//计算总价格
        public decimal TotalCost
        
{
            
get {
                
decimal Total = 0;
              
foreach (DictionaryEntry entry in Cart_Orders)
                
{
                    Stat_Class order 
= (Stat_Class)entry.Value;
                    Total 
+= (order.Price * order.Quantity);
                }

                
return Total;
                }

         }

         
//添加物件方法
        public void AddItem(Stat_Class Order)
        
//添加物件方法
        Stat_Class order=( Stat_Class)Cart_Orders[Order.ItemID];
         
if(order!=null)
         order.Quantity
+=1;
     
else
       Cart_Orders.Add(Order.ItemID,Order);
     }


        
//删除物件
        public void DeleteItem(string ItemID)
        
{
            
if (Cart_Orders[ItemID] != null)
            
{
                Cart_Orders.Remove(ItemID);
            }

        }

    }

}


Main.aspx页面

<style type="text/css">
        table
{font-size:12px;}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div style="text-align:center;">
        
<table >
            
<tr>
                
<td align="center" style="width: 422px; font-weight: bold; font-size: 18px; color: #ff3333;">
                    欢 迎 光 临 本 站 购 物 
!</td>
            
</tr>
            
<tr>
                
<td align="left" style="width: 422px; height: 8px; font-size: 12px; color: #0033cc;">
                    下面是你可以选择的商品种类:
</td>
            
</tr>
            
<tr>
                
<td align="left" style="width: 422px; height: 260px;" valign="top">
                    
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataSourceID="SqlDataSource1">
                        
<Columns>
                            
<asp:TemplateField>
                                
<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="20px" />
                                
<ItemTemplate>
                                    #
                                
</ItemTemplate>
                            
</asp:TemplateField>
                            
<asp:HyperLinkField DataNavigateUrlFields="petTypeID" DataNavigateUrlFormatString="showPetByTypeID.aspx?petID={0}"
                                DataTextField
="petTypeName" HeaderText="宠物类别">
                                
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
                                
<HeaderStyle ForeColor="Fuchsia" />
                            
</asp:HyperLinkField>
                        
</Columns>
                    
</asp:GridView>
                
</td>
            
</tr>
        
</table>
     
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ShoppingBusConnectionString %>"
            SelectCommand
="SELECT * FROM [petType]"></asp:SqlDataSource>
    
</div>
    
</form>
</body>


showPetByTypeID.aspx页面

<title>无标题页</title>
    
<style type="text/css">
        table
{font-size:12px;}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div style="text-align: center">
        
<table>
            
<tr>
                
<td align="left" style="width: 469px">
                    类别名称为: 
&nbsp; &nbsp;
                    
<asp:Label ID="Label1" runat="server" Text="Label" Width="72px"></asp:Label></td>
            
</tr>
            
<tr>
                
<td style="width: 469px; height: 324px">
                    
<asp:GridView ID="GridView1" DataKeyNames="petID" runat="server" Height="256px" Width="100%" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                        
<Columns>
                            
<asp:BoundField DataField="petPhoto" DataFormatString="&lt;img src='img/{0}' width='30' height='50' alt='图片地址:{0}'&gt;"
                                HeaderText
="宠物照片">
                                
<ItemStyle Width="60px" />
                            
</asp:BoundField>
                            
<asp:BoundField DataField="petName" HeaderText="宠物名称">
                                
<ItemStyle Width="100px" />
                            
</asp:BoundField>
                            
<asp:BoundField DataField="petPrice" DataFormatString="{0:c}" HeaderText="售价" HtmlEncode="False">
                                
<ItemStyle Width="50px" />
                            
</asp:BoundField>
                            
<asp:BoundField DataField="petRemark" HeaderText="描述">
                                
<ItemStyle Width="250px" />
                            
</asp:BoundField>
                         
<asp:ButtonField HeaderText="订  购"  ButtonType="Link" Text="加入购物车"  CommandName="select">
                                
<ItemStyle Width="80px" />
                            
</asp:ButtonField >
                        
</Columns>
                    
</asp:GridView>
                
</td>
            
</tr>
            
<tr>
                
<td style="width: 469px; height: 24px;">
                    
<asp:LinkButton ID="LinkButton1" runat="server" Width="64px" OnClick="LinkButton1_Click">查看购物车</asp:LinkButton></td>
            
</tr>
        
</table>
    
    
</div>
    
</form>


showPetByTypeID.aspx.cs

using System.Data.SqlClient;
using WendwCart;

public partial class showPetByTypeID : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!this.IsPostBack)
        
{
            GridView1.DataSource 
= bind();
            GridView1.DataBind();
        }

    }

    
private SqlDataReader bind()
    
{
        
string petID = Request.QueryString["petID"].ToString();
        
string connection = ConfigurationManager.ConnectionStrings["ShoppingBusConnectionString"].ConnectionString;
        SqlConnection con 
= new SqlConnection(connection);
        con.Open();
        SqlCommand cmd 
= new SqlCommand("select petName from pet where petTypeID='" + petID + "'", con);
        Label1.Text 
= Convert.ToString(cmd.ExecuteScalar());
        cmd.CommandText 
= "select * from pet where petTypeID='" + petID + "'";
        SqlDataReader sdr 
= cmd.ExecuteReader();
        
return sdr;
    }

    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
string petID = GridView1.SelectedDataKey.Value.ToString();
        GridViewRow row 
= GridView1.SelectedRow;
        
//格式后的Price是一个纯数字 如:100
        
//格式前的是¥50的(不能用来计算)  
        decimal Price = decimal.Parse(row.Cells[2].Text, System.Globalization.NumberStyles.Currency);
        
string Name=row.Cells[1].Text.ToString();
        Stat_Class order 
= new Stat_Class(petID, Name, Price, 1);
        
//因为在Global中指定了这里就不用指定了
        
//每个进网站的人都创建一个购物车
        
//if (Session["MyShoppingCart"] == null)
        
//{
        
//    Session["MyShoppingCart"] = new ShoppingCart();
        
//}
        ShoppingCart Cart = (ShoppingCart)Session["MyShoppingCart"];
        
if (Cart != null)
        
{
            Cart.AddItem(order);
        }

    }

    
protected void LinkButton1_Click(object sender, EventArgs e)
    
{
        Response.Redirect(
"View_ShoppingCart.aspx");
    }

}



View_ShoppingCart.aspx页面:

 <div style="text-align: center">
    
<table width="100%">
     
<tr>
       
<td style="height: 38px">
           
&nbsp;<asp:LinkButton ID="lbtn" Text="继续购物" OnClick="lbtn_Click" runat="server" style="font-weight: bold; font-size: 20px; color: #0000ff" Height="16px" Width="96px"/></td>
     
</tr>
   
</table>

    
    
</div>
        
<br />
        
<br />
        
<table>
            
<tr>
                
<td align="center" valign="middle" style="font-weight: bold; font-size: 18px; color: #ff3366">
                    购物清单:
</td>
            
</tr>
            
<tr>
                
<td style="width: 472px; height: 143px" valign="top">
                    
<asp:GridView ID="GridView1" DataKeyNames="ItemID" runat="server" Width="100%" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    
<Columns>
                        
<asp:BoundField HeaderText="宠物ID" DataField="ItemID" />
                        
<asp:BoundField HeaderText="宠物名称" DataField="ShangpinName" />
                        
<asp:BoundField HeaderText="售  价" DataField="Price" DataFormatString="{0:c}" >
                            
<ItemStyle HorizontalAlign="Center" />
                            
<HeaderStyle HorizontalAlign="Center" />
                        
</asp:BoundField>
                        
<asp:BoundField HeaderText="数  量" DataField="Quantity" >
                            
<ItemStyle HorizontalAlign="Center" />
                            
<HeaderStyle HorizontalAlign="Center" />
                        
</asp:BoundField>
                        
<asp:ButtonField HeaderText="删  除" Text="删除" CommandName="select" >
                            
<ItemStyle HorizontalAlign="Center" />
                            
<HeaderStyle HorizontalAlign="Center" />
                        
</asp:ButtonField> 
                    
</Columns>
                    
</asp:GridView>
                
</td>
            
</tr>
             
<tr>
                
<td align="left" valign="middle" style="font-weight: normal; font-size: 18px; color: #330033; height: 13px;">
                    说明:
                    
<asp:Label ID="Total" runat="server" Text="Label" Width="112px"></asp:Label></td>
            
</tr>
        
</table>
    
</form>
</body>


View_ShoppingCart.aspx.cs页面:
using WendwCart;
public partial class View_ShoppingCart : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//创建实例
        ShoppingCart cart = (ShoppingCart)Session["MyShoppingCart"];
        
if (cart != null)
        
{
            GridView1.DataSource 
= cart.Orders;
            GridView1.DataBind();
            Total.Text 
= String.Format("合计:{0:c}", cart.TotalCost);
        }

    }

    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        ShoppingCart cart 
= (ShoppingCart)Session["MyShoppingCart"];
        
string pid = GridView1.SelectedDataKey.Value.ToString();
        
if (cart != null)
        
{
            cart.DeleteItem(pid);
            Total.Text 
= String.Format("合计:{0:c}", cart.TotalCost);
            GridView1.DataBind();
        }

    }

    
protected void lbtn_Click(object sender, EventArgs e)
    
{
        Response.Redirect(
"Main.aspx");
    }

}


SQL文件:

Create database ShoppingBus
go
use ShoppingBus
go
--宠物类别
Create table petType
(
 petTypeID varchar(10) primary key, --类别编号
 petTypeName varchar(50) not null unique
)
go
--插入测试数据
insert into petType Values('pt1001','鸟类')
insert into petType Values('pt1002','狗类')
insert into petType Values('pt1003','猫类')
go
--select * from petType
--宠物表
Create table pet
(
 petID varchar(20) primary key, --宠物编号
 petName varchar(100) not null, --宠物名称
 petTypeID varchar(10) foreign key references  petType(petTypeID),
 petPrice Money,   --售价
 petPhoto varchar(30),  --照片
 petRemark varchar(1000)  --描述 
)
go
--插入测试数据(宠物表)
insert into pet values('pet1001','波斯猫1','pt1003',100,'1.gif','印度波斯猫111')
insert into pet values('pet1002','波斯猫2','pt1003',50,'2.gif','印度波斯猫222')
insert into pet values('pet1003','波斯猫3','pt1003',800,'3.gif','印度波斯猫333')
insert into pet values('pet1004','波斯猫4','pt1003',780,'4.gif','印度波斯猫444')

Web.config

<configuration>
    
<appSettings/>
    
<connectionStrings>
        
<add name="ShoppingBusConnectionString" connectionString="Data Source=.;Initial Catalog=ShoppingBus;Integrated Security=True" providerName="System.Data.SqlClient"/>
    
</connectionStrings>
posted on 2007-01-01 01:48  小角色  阅读(1779)  评论(2)    收藏  举报