首页代码:

Main.aspx:
 1head runat="server">
 2    <title>无标题页</title>
 3    <style type="text/css">
 4        table{font-size:12px}
 5    </style>
 6</head>
 7<body>
 8    <form id="form1" runat="server">
 9    <div style="text-align:center;">
10        <table style="width: 496px; height: 224px;">
11            <tr>
12                <td style="width: 100px">
13                    <asp:Label ID="Label1" runat="server" Text="欢迎光临" Width="304px"></asp:Label></td>
14            </tr>
15            <tr>
16                <td style="width: 100px; text-align: left; height: 16px;">
17                    <asp:Label ID="Label2" runat="server" Text="你可以选购的商品种类" Width="200px"></asp:Label></td>
18            </tr>
19            <tr>
20                <td style="width: 100px; height: 142px;" valign="top">
21                    <asp:GridView ID="GridView1" runat="server" Width="100%" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="petTypeID" DataSourceID="SqlDataSource1">
22                        <Columns>
23                            <asp:TemplateField ItemStyle-Width="10px" ItemStyle-HorizontalAlign="Center" >
24                                <ItemTemplate>
25                                    #
26                                </ItemTemplate>
27                            </asp:TemplateField>
28                            <asp:HyperLinkField DataNavigateUrlFields="petTypeID" DataNavigateUrlFormatString="showPetByTypeID.aspx?petID={0}"
29                                DataTextField="petTypeName" Target="_blank" />
30                        </Columns>
31                        <RowStyle Height="20px" />
32                    </asp:GridView>
33                </td>
34            </tr>
35        </table>
36    
37    </div>
38        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ShoppingBusConnectionString %>"
39            SelectCommand="SELECT * FROM [petType]"></asp:SqlDataSource>
40    </form>
41</body>
42</html>
43


商品页代码: showPetByTypeID.aspx
 1  <style type="text/css">
 2        table{font-size:12px}
 3    </style>
 4</head>
 5<body>
 6    <form id="form1" runat="server">
 7    <div style="text-align:center">
 8        <table>
 9            <tr>
10                <td style="width: 463px; text-align: left;">
11                    类别名称:<asp:Label ID="Label1" runat="server" Text="Label" Width="104px"></asp:Label></td>
12            </tr>
13            <tr>
14                <td style="width: 463px; height: 142px;" valign="top">
15                    <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False" DataKeyNames="petID" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
16                        <Columns>
17                            <asp:BoundField DataField="petPhoto" DataFormatString="&lt;img src='img/{0}' width='30px' height='50px'&gt;&lt;/img&gt;"
18                                HeaderText="宠物照片" >
19                                <ItemStyle Width="60px" />
20                            </asp:BoundField>
21                            <asp:BoundField DataField="petName" HeaderText="宠物名称" >
22                                <ItemStyle Width="100px" />
23                            </asp:BoundField>
24                            <asp:BoundField DataField="petPrice" DataFormatString="{0:c}" HeaderText="售价" HtmlEncode="False" >
25                                <ItemStyle Width="50px" />
26                            </asp:BoundField>
27                            <asp:BoundField DataField="petRemark" HeaderText="描述" >
28                                <ItemStyle Width="250px" />
29                            </asp:BoundField>
30                            <asp:ButtonField Text="购买" CommandName="select" >
31                                <ItemStyle Width="50px" />
32                            </asp:ButtonField>
33                        </Columns>
34                    </asp:GridView>
35                </td>
36            </tr>
37            <tr>
38                <td style="width: 463px; height: 38px;">
39                    <asp:LinkButton ID="btnShowBus" runat="server" Width="96px" OnClick="btnShowBus_Click">查看购物车</asp:LinkButton></td>
40            </tr>
41        </table>
42    
43    </div>
44    </form>


商品页showPetByTypeID.aspx.cs代码

 1public partial class showPetByTypeID : System.Web.UI.Page
 2{
 3    protected void Page_Load(object sender, EventArgs e)
 4    {
 5        if (!this.IsPostBack)
 6        {
 7            string PetTypeID = Request.QueryString["petID"].ToString();
 8            string connectionString = ConfigurationManager.ConnectionStrings["ShoppingBusConnectionString"].ConnectionString;
 9            SqlConnection con = new SqlConnection(connectionString);
10            con.Open();
11            SqlCommand cmd = new SqlCommand("select petName from pet where petTypeID='"+PetTypeID+"'", con);
12            Label1.Text = Convert.ToString(cmd.ExecuteScalar());
13            cmd.CommandText = "select * from pet where petTypeID='" + PetTypeID + "'";
14            SqlDataReader sdr = cmd.ExecuteReader();
15            GridView1.DataSource = sdr;
16            GridView1.DataBind();
17        }

18    }

19    //protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
20    //{
21    //    if (e.CommandName == "AddToBus")
22    //    {
23    //        string petID = this.GridView1.SelectedDataKey.Value.ToString();
24    //        if (Session["bus"] == null)
25    //        {
26    //            Hashtable ht = new Hashtable();
27    //            ht.Add(petID, 1);
28    //            Session["bus"] = ht;
29    //            //Session.Add("bus", ht);
30    //        }
31    //        else
32    //        {
33    //            Hashtable ht = (Hashtable)Session["bus"];
34    //            if (ht[petID] == null)
35    //            {
36    //                ht[petID] = 1;
37    //            }
38    //            else
39    //            {
40    //                ht[petID] = (int)ht[petID] + 1;
41    //            }
42    //            Session["bus"] = ht;
43    //        }
44    //    }
45    //}
46    protected void btnShowBus_Click(object sender, EventArgs e)
47    {
48        Response.Redirect("ShowBus.aspx");
49    }

50    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
51    {
52        string petID = this.GridView1.SelectedDataKey.Value.ToString();
53        if (Session["bus"== null)
54        {
55            Hashtable ht = new Hashtable();
56            ht.Add(petID, 1);
57            Session["bus"= ht;
58            //Session.Add("bus", ht);
59        }

60        else
61        {
62            Hashtable ht = (Hashtable)Session["bus"];
63            if (ht[petID] == null)
64            {
65                ht[petID] = 1;
66            }

67            else
68            {
69                ht[petID] = (int)ht[petID] + 1;
70            }

71            Session["bus"= ht;
72        }

73    }

74}

75

购物车页代码ShowBus.aspx

 1    <form id="form1" runat="server">
 2    <div style="text-align: center">
 3        <asp:DataList ID="DataList1" runat="server" Width="376px">
 4        <ItemTemplate>
 5            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Key") %>' Width="56px"></asp:Label>-
 6            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Value") %>' Visible="false"></asp:TextBox>
 7            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Value") %>' Width="88px"></asp:Label>
 8        </ItemTemplate>
 9            <SeparatorTemplate>
10                <hr style="color:Red;" />
11            </SeparatorTemplate>
12            <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
13                Font-Underline="False" HorizontalAlign="Center" VerticalAlign="Top" />
14            <SeparatorStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
15                Font-Underline="False" ForeColor="#FF66CC" HorizontalAlign="Center" VerticalAlign="Top" />
16        </asp:DataList></div>
17        <br />
18        <br />
19        <asp:LinkButton ID="LinkButton2" runat="server" Width="72px" OnClick="LinkButton2_Click">编辑数量</asp:LinkButton>
20        &nbsp; &nbsp; &nbsp;<asp:LinkButton ID="LinkButton1" runat="server"  Width="88px" OnClick="LinkButton1_Click">更新购物车</asp:LinkButton>
21    </form>

ShowBus.aspx.cs代码:

 1public partial class ShowBus : System.Web.UI.Page
 2{
 3    protected void Page_Load(object sender, EventArgs e)
 4    {
 5        if (!this.IsPostBack)
 6        {
 7            DataList1.DataSource = (Hashtable)Session["bus"];
 8            DataList1.DataBind();
 9        }

10    }

11    protected void LinkButton1_Click(object sender, EventArgs e)
12    {
13        //遍历集合
14        foreach (DataListItem dl in DataList1.Items)
15        {
16            //找到文本框
17            TextBox tb = (TextBox)dl.FindControl("TextBox1");
18            //查找文本筐的值
19            int newpid = Convert.ToInt32(tb.Text.ToString());
20            //找到装载哈希表key字段的那个控件
21            Label lb = (Label)dl.FindControl("Label1");
22            //把他的值拿出来
23            string pid = lb.Text.ToString();
24            //把session["bus"]对象赋值给哈希表
25            Hashtable hs = (Hashtable)Session["bus"];
26            //求得原来的数量
27            int oldpid = (int)hs[pid];
28            //判断以前文本框的值和新文本框的值是否相同
29            if (newpid != oldpid)
30            {
31                hs[pid] = newpid;
32            }

33            //最后再更新Session 对象
34            Session["bus"= hs;
35        }

36    }

37    //实现自定义编辑
38    protected void LinkButton2_Click(object sender, EventArgs e)
39    {
40        foreach(DataListItem dl in DataList1.Items)
41        {
42            TextBox txtValue = (TextBox)dl.FindControl("TextBox1");
43            txtValue.Visible = true;
44            Label lb = (Label)dl.FindControl("Label2");
45            lb.Visible = false;
46        }

47    }

48}

注意选择按钮的Command属性一定要是"select"啊 这样才能执行Grid1_SelectIndexChanged事件
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')
posted on 2006-12-30 03:12  小角色  阅读(232)  评论(0)    收藏  举报