小莲莲
学如初出之苗,不见其增日有所长;惰如磨刀之石,不见其损日有所亏!
在商品展示页点击购买跳到购物车页面,用一个DataTable将购买的商品存放起来,并可以删除或更改购买数量、合计出最终购物车的小计及总计。
显示商品页面
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="shopCart.aspx.cs" Inherits="shopCart" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <title>无标题页</title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <table border="1" width="100%">
12         <tr>
13             <td>
14                 <table>
15                     <tr>
16                         <td>类别名称:</td>
17                     </tr>
18                     <asp:Repeater ID="RepProductType" runat="server">
19                         <ItemTemplate>
20                             <tr>
21                                 <td><a href='<%#"shopCart.aspx?productTypeID="+Eval("productTypeID")%>'><%#Eval("productTypeName"%></a></td>
22                             </tr>
23                         </ItemTemplate>
24                     </asp:Repeater>
25                 </table>
26             </td>
27             <td>
28                  <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" 
29                           RepeatDirection="Horizontal" Width="760px">
30                         <ItemTemplate>
31                           <table border="0" cellpadding="3" cellspacing="2">
32                               <tr>
33                                 <td colspan="2"><img src='UploadImages/<%#Eval("productPic") %>' width="150" height="150" /></td>
34                               </tr>
35                               <tr>
36                                 <td><%#Eval("productName")%></td>
37                                 <td><a href='<%#"shopCart1.aspx?productID="+Eval("productID") %>'>购买</a></td>
38                               </tr>
39                           </table>
40                         </ItemTemplate>
41                   </asp:DataList>
42             </td>
43         </tr>
44     </table>
45     </form>
46 </body>
47 </html>
购物车前台代码
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="shopCart1.aspx.cs" Inherits="shopCart1" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <title>无标题页</title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <table border="1">
12         <tr>
13             <td>商品号</td>
14             <td>商品名</td>
15             <td>单价</td>
16             <td>类型号</td>
17             <td>图片</td>
18             <td>购买数量</td>
19             <td>小计</td>
20             <td>更改数量</td>
21             <td>放回原处</td>
22         </tr>
23         <asp:Repeater ID="Repeater1" runat="server" 
24             onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound">
25             <ItemTemplate>
26                 <tr>
27                     <td>
28                         <asp:Literal ID="LitProductID" runat="server" Text='<%#Eval("productID")%>'></asp:Literal></td>
29                     <td><%#Eval("productName")%></td>
30                     <td><%#Eval("unitPrice")%></td>
31                     <td><%#Eval("productTypeID")%></td>
32                     <td><img width="80" height="80" src='<%#"UploadImages/"+Eval("productPic") %>' /></td>
33                     <td id="TDcount" runat="server">
34                         <asp:Label ID="LblBuyCount" runat="server" Text='<%#Eval("buyCount")%>'></asp:Label>
35                         <asp:TextBox ID="TxtBuyCount" Visible="false" runat="server" Text='<%#Eval("buyCount")%>'></asp:TextBox>
36                     </td>
37                     <td><%#Eval("subtotal")%></td>
38                     <td>
39                         <asp:HyperLink ID="HLUpdateBuyCount" runat="server" NavigateUrl='<%#"shopCart1.aspx?cartProductID="+Eval("productID") %>'>更改数量</asp:HyperLink>
40                         <asp:LinkButton ID="LBtnUpdate" runat="server" Visible="false" onclick="LBtnUpdate_Click">更改</asp:LinkButton>
41                         <asp:LinkButton ID="LBtnCancel" runat="server" Visible="false"  onclick="LBtnCancel_Click">取消</asp:LinkButton>
42                     </td>
43                     <td><asp:ImageButton ID="ImgDel" title="删除" CommandName="delete" ImageUrl="Admin/HXImages/del.gif" runat="server" /></td>
44                 </tr>
45             </ItemTemplate>
46         </asp:Repeater>
47         <tr>
48             <td colspan="8" align="right">总计:</td>
49             <td>
50                 <asp:Label ID="LblTotal" runat="server" Text="Label"></asp:Label></td>
51         </tr>
52     </table>
53     <a href="shopCart.aspx">继续购物</a>
54     </form>
55 </body>
56 </html>
57 
购物车后台代码
  1 using System;
  2 using System.Collections;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.Security;
  8 using System.Web.UI;
  9 using System.Web.UI.HtmlControls;
 10 using System.Web.UI.WebControls;
 11 using System.Web.UI.WebControls.WebParts;
 12 using System.Xml.Linq;
 13 using System.Text;
 14 
 15 public partial class ShopCart2 : System.Web.UI.Page
 16 {
 17     string sql = null;
 18     DataSet ds = new DataSet();
 19     protected void Page_Load(object sender, EventArgs e)
 20     {
 21         if (!IsPostBack)
 22         {
 23             if (productID != null)
 24             {
 25                 GetShopCart();
 26             }
 27             else
 28             {
 29                 Response.Redirect("shopCart.aspx");
 30             }
 31         }
 32     }
 33     #region//Create a shopCart and put the product into it
 34     void GetShopCart()
 35     {
 36         if (Session["shopCart"== null || Session["shopCart"== "")
 37         {
 38             //there has no cart
 39             CreateCartTable();
 40         }
 41         //there has a cart
 42         PutInShopCart();
 43     }
 44     void PutInShopCart()
 45     {
 46         DataTable dt = new DataTable("myTable");
 47         dt = (DataTable)Session["shopCart"];
 48         bool hasone = false;
 49         foreach (DataRow dr in dt.Rows)
 50         {
 51             if (int.Parse(productID) == Convert.ToInt32(dr["productID"]))
 52             {
 53                 //select the same product
 54                 //buyCount plus one
 55                 dr["buyCount"= Convert.ToInt32(dr["buyCount"]) + 1;
 56                 dr["subtotal"= Convert.ToInt32(dr["buyCount"]) * Convert.ToDecimal(dr["unitPrice"]);
 57                 hasone = true;
 58                 break;
 59             }
 60             else
 61             {
 62                 hasone = false;
 63             }
 64         }
 65         if (hasone == false)
 66         {
 67             //put the product into dt
 68             sql = string.Format("select * from productTable where productID={0}"int.Parse(Request.QueryString["productID"]));
 69             DataTable productTable = DataBase.ExecuteDataSet(sql).Tables[0];
 70             DataRow rows;
 71             rows = new DataTable("cartTable").NewRow();
 72             int productID = Convert.ToInt32(productTable.Rows[0]["productID"]);
 73             string productName = productTable.Rows[0]["productID"].ToString();
 74             Decimal unitPrice = Convert.ToDecimal(productTable.Rows[0]["unitPrice"]);
 75             int productTypeID = Convert.ToInt32(productTable.Rows[0]["productTypeID"]);
 76             string productPic = productTable.Rows[0]["productPic"].ToString();
 77             int buyCount = 1;
 78             Decimal subtotal = buyCount * unitPrice;
 79             dt.Rows.Add(productID, productName, unitPrice, productTypeID, productPic, buyCount, subtotal);
 80         }
 81         Session["shopCart"= dt;
 82         Repeater1.DataSource = dt.DefaultView;
 83         Repeater1.DataBind();
 84         GetTotal();
 85     }
 86     void CreateCartTable()
 87     {
 88         DataTable dtCart = new DataTable("cartTable");
 89         DataColumn dc;
 90         dc = new DataColumn("productID", System.Type.GetType("System.Int32"));
 91         dtCart.Columns.Add(dc);
 92         dc = new DataColumn("productName", System.Type.GetType("System.String"));
 93         dtCart.Columns.Add(dc);
 94         dc = new DataColumn("unitPrice", System.Type.GetType("System.Decimal"));
 95         dtCart.Columns.Add(dc);
 96         dc = new DataColumn("productTypeID", System.Type.GetType("System.Int32"));
 97         dtCart.Columns.Add(dc);
 98         dc = new DataColumn("productPic", System.Type.GetType("System.String"));
 99         dtCart.Columns.Add(dc);
100         dc = new DataColumn("buyCount", System.Type.GetType("System.Int32"));
101         dtCart.Columns.Add(dc);
102         dc = new DataColumn("subtotal", System.Type.GetType("System.Decimal"));
103         dtCart.Columns.Add(dc);
104         ds.Tables.Add(dtCart);
105 
106         //DataRow dr;
107         //dr = new DataTable("cartTable").NewRow();
108         //int productID = Convert.ToInt32(ds.Tables["productTable"].Rows[0]["productID"]);
109         //string productName = ds.Tables["productTable"].Rows[0]["productID"].ToString();
110         //Decimal unitPrice = Convert.ToDecimal(ds.Tables["productTable"].Rows[0]["unitPrice"]);
111         //int productTypeID = Convert.ToInt32(ds.Tables["productTable"].Rows[0]["productTypeID"]);
112         //string productPic = ds.Tables["productTable"].Rows[0]["productPic"].ToString();
113         //int buyCount = 1;
114         //Decimal subtotal = buyCount * unitPrice;
115         //dtCart.Rows.Add(productID, productName, unitPrice, productTypeID, productPic, buyCount, subtotal);
116         Session["shopCart"= dtCart;
117         Repeater1.DataSource = dtCart.DefaultView;
118         Repeater1.DataBind();
119     }
120     #endregion
121     protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
122     {
123         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
124         {
125             ((ImageButton)e.Item.FindControl("ImgDel")).Attributes.Add("onclick""return confirm('确定要删除吗?')");
126             string nowProID = ((Literal)e.Item.FindControl("LitProductID")).Text.ToString();
127             ((TextBox)e.Item.FindControl("TxtBuyCount")).Attributes.Add("onfocus""TxtBuyCountOnfocus(" + nowProID + ")");
128         }
129     }
130     protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
131     {
132         int proID = Convert.ToInt32(((Literal)e.Item.FindControl("LitProductID")).Text.ToString());
133         DataTable dt = (DataTable)Session["shopCart"];
134         for (int i = 0; i < dt.Rows.Count; i++)
135         {
136             if (Convert.ToInt32(dt.Rows[i]["productID"]) == proID)
137             {
138                 //delete this row
139                 dt.Rows.RemoveAt(i);
140             }
141         }
142         Session["shopCart"= dt;
143         Repeater1.DataSource = dt.DefaultView;
144         Repeater1.DataBind();
145         GetTotal();
146     }
147     void GetTotal()
148     {
149         decimal total = 0;
150         DataTable dt = (DataTable)Session["shopCart"];
151         for (int i = 0; i < dt.Rows.Count; i++)
152         {
153             total += Convert.ToDecimal(dt.Rows[i]["subtotal"]);
154         }
155         LblTotal.Text = total.ToString();
156     }
157     #region//update your buy products' count
158     protected void TxtBuyCount_TextChanged(object sender, EventArgs e)
159     {
160         DataTable dt = (DataTable)Session["shopCart"];
161         //找出当前文本框所在行的productID来
162         int cartProductID = Convert.ToInt32(TxtProductID.Text);
163         bool b = true;
164         for (int i = 0; i < Repeater1.Items.Count; i++)
165         {
166             if (Convert.ToInt32(((Literal)Repeater1.Items[i].FindControl("LitProductID")).Text) == cartProductID)
167             {
168                 try
169                 {
170                     int nowBuyCount = Convert.ToInt32(((TextBox)Repeater1.Items[i].FindControl("TxtBuyCount")).Text);
171                     dt.Rows[i]["buyCount"= nowBuyCount.ToString();
172                     dt.Rows[i]["subtotal"= Convert.ToInt32(dt.Rows[i]["unitPrice"]) * nowBuyCount;
173                 }
174                 catch
175                 {
176                     Response.Write("<script>alert('数量输入有误!');</script>");
177                     b = false;
178                 }
179                 Repeater1.DataSource = dt.DefaultView;
180                 Repeater1.DataBind();
181                 GetTotal();
182                 if (b)
183                 {
184                     Response.Write("<script>alert('数量修改成功!');</script>");
185                 }
186             }
187         }
188     }
189     #endregion
190 
191     public string productID
192     {
193         get
194         {
195             return Request.QueryString["productID"];
196         }
197     }
198 }
199 


  1 using System;
  2 using System.Collections;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.Security;
  8 using System.Web.UI;
  9 using System.Web.UI.HtmlControls;
 10 using System.Web.UI.WebControls;
 11 using System.Web.UI.WebControls.WebParts;
 12 using System.Xml.Linq;
 13 
 14 public partial class shopCart1 : System.Web.UI.Page
 15 {
 16     string sql = null;
 17     DataSet ds = new DataSet();
 18     protected void Page_Load(object sender, EventArgs e)
 19     {
 20         if (!IsPostBack)
 21         {
 22             if (productID != null)
 23             {
 24                 GetShopCart();
 25             }
 26             else
 27             {
 28                 if (cartProductID != null && cartProductID != "")
 29                 {
 30                     //update product's buyCount
 31                     UpdateBuyCount();
 32                 }
 33                 else
 34                 {
 35                     Response.Redirect("shopCart.aspx");
 36                 }
 37             }
 38         }
 39     }
 40     #region//Create a shopCart and put the product into it
 41     void GetShopCart()
 42     {
 43         if (Session["shopCart"== null||Session["shopCart"=="")
 44         {
 45             //there has no cart
 46             CreateCartTable();
 47         }
 48         PutInShopCart();
 49     }
 50     void PutInShopCart()
 51     {
 52         DataTable dt = new DataTable("myTable");
 53         dt = (DataTable)Session["shopCart"];
 54         bool hasone = false;
 55         foreach (DataRow dr in dt.Rows)
 56         {
 57             if (int.Parse(productID) == Convert.ToInt32(dr["productID"])) 
 58             {
 59                 //select the same product,buyCount plus one
 60                 dr["buyCount"= Convert.ToInt32(dr["buyCount"]) + 1;
 61                 dr["subtotal"= Convert.ToInt32(dr["buyCount"]) * Convert.ToDecimal(dr["unitPrice"]);
 62                 hasone = true;
 63                 break;
 64             }
 65             else
 66             {
 67                 hasone = false;
 68             }
 69         }
 70         if (hasone == false)
 71         {
 72             //put the product into dt
 73             sql = string.Format("select * from productTable where productID={0}"int.Parse(Request.QueryString["productID"]));
 74             DataTable productTable = DataBase.ExecuteDataSet(sql).Tables[0];
 75             DataRow rows;
 76             rows = new DataTable("cartTable").NewRow();
 77             int productID = Convert.ToInt32(productTable.Rows[0]["productID"]);
 78             string productName = productTable.Rows[0]["productID"].ToString();
 79             Decimal unitPrice = Convert.ToDecimal(productTable.Rows[0]["unitPrice"]);
 80             int productTypeID = Convert.ToInt32(productTable.Rows[0]["productTypeID"]);
 81             string productPic = productTable.Rows[0]["productPic"].ToString();
 82             int buyCount = 1;
 83             Decimal subtotal = buyCount * unitPrice;
 84             dt.Rows.Add(productID, productName, unitPrice, productTypeID, productPic, buyCount,subtotal);
 85         }
 86         Session["shopCart"= dt;
 87         Repeater1.DataSource = dt.DefaultView;
 88         Repeater1.DataBind();
 89         GetTotal();
 90     }
 91     void CreateCartTable()
 92     {
 93         DataTable dtCart = new DataTable("cartTable");
 94         DataColumn dc ;
 95         dc = new DataColumn("productID", System.Type.GetType("System.Int32"));
 96         dtCart.Columns.Add(dc);
 97         dc = new DataColumn("productName", System.Type.GetType("System.String"));
 98         dtCart.Columns.Add(dc);
 99         dc = new DataColumn("unitPrice", System.Type.GetType("System.Decimal"));
100         dtCart.Columns.Add(dc);
101         dc = new DataColumn("productTypeID", System.Type.GetType("System.Int32"));
102         dtCart.Columns.Add(dc);
103         dc = new DataColumn("productPic", System.Type.GetType("System.String"));
104         dtCart.Columns.Add(dc);
105         dc = new DataColumn("buyCount", System.Type.GetType("System.Int32"));
106         dtCart.Columns.Add(dc);
107         dc = new DataColumn("subtotal", System.Type.GetType("System.Decimal"));
108         dtCart.Columns.Add(dc);
109         ds.Tables.Add(dtCart);
110 
111         Session["shopCart"= dtCart;
112         Repeater1.DataSource = dtCart.DefaultView;
113         Repeater1.DataBind();
114     }
115     #endregion
116     protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
117     {
118         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
119         {
120             ((ImageButton)e.Item.FindControl("ImgDel")).Attributes.Add("onclick""return confirm('确定要删除吗?')");
121         }
122     }
123     protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
124     {
125         int proID = Convert.ToInt32(((Literal)e.Item.FindControl("LitProductID")).Text.ToString());
126         DataTable dt = (DataTable)Session["shopCart"];
127         for (int i = 0; i < dt.Rows.Count;i++ )
128         {
129             if (Convert.ToInt32(dt.Rows[i]["productID"]) == proID)
130             {
131                 //delete this row
132                 dt.Rows.RemoveAt(i);
133             }
134         }
135         Session["shopCart"= dt;
136         Repeater1.DataSource = dt.DefaultView;
137         Repeater1.DataBind();
138         GetTotal();
139     }
140     void GetTotal()
141     {
142         decimal total = 0;
143         DataTable dt = (DataTable)Session["shopCart"];
144         for (int i = 0; i < dt.Rows.Count; i++)
145         {
146             total += Convert.ToDecimal(dt.Rows[i]["subtotal"]);
147         }
148         LblTotal.Text = total.ToString();
149     }
150     #region//update your buy products' count
151     void UpdateBuyCount()
152     {
153         DataTable dt = (DataTable)Session["shopCart"];
154         Repeater1.DataSource = dt.DefaultView;
155         Repeater1.DataBind();
156         GetTotal();
157         for (int i = 0; i < Repeater1.Items.Count; i++)
158         {
159             if (Convert.ToInt32(((Literal)Repeater1.Items[i].FindControl("LitProductID")).Text) == int.Parse(cartProductID))
160             {
161                 ((Label)Repeater1.Items[i].FindControl("LblBuyCount")).Visible = false;
162                 ((TextBox)Repeater1.Items[i].FindControl("TxtBuyCount")).Visible = true;
163                 ((HyperLink)Repeater1.Items[i].FindControl("HLUpdateBuyCount")).Visible = false;
164                 ((LinkButton)Repeater1.Items[i].FindControl("LBtnUpdate")).Visible = true;
165                 ((LinkButton)Repeater1.Items[i].FindControl("LBtnCancel")).Visible = true;
166             }
167         }
168     }
169     protected void LBtnUpdate_Click(object sender, EventArgs e)
170     {
171         //get the Textbox's count
172         DataTable dt = (DataTable)Session["shopCart"];
173         for (int i = 0; i < Repeater1.Items.Count; i++)
174         {
175             if (Convert.ToInt32(((Literal)Repeater1.Items[i].FindControl("LitProductID")).Text) == int.Parse(cartProductID))
176             {
177                 int nowBuyCount = Convert.ToInt32(((TextBox)Repeater1.Items[i].FindControl("TxtBuyCount")).Text);
178                 dt.Rows[i]["buyCount"= nowBuyCount.ToString();
179                 dt.Rows[i]["subtotal"= Convert.ToInt32(dt.Rows[i]["unitPrice"]) * nowBuyCount;
180                 Repeater1.DataSource = dt.DefaultView;
181                 Repeater1.DataBind();
182                 GetTotal();
183             }
184         }
185     }
186     protected void LBtnCancel_Click(object sender, EventArgs e)
187     {
188         DataTable dt = (DataTable)Session["shopCart"];
189         Repeater1.DataSource = dt.DefaultView;
190         Repeater1.DataBind();
191         GetTotal();
192     }
193     #endregion
194 
195     public string productID
196     {
197         get
198         {
199             return Request.QueryString["productID"];
200         }
201     }
202     public string cartProductID
203     {
204         get
205         {
206             return Request.QueryString["cartProductID"];
207         }
208     }
209 }
210 


posted on 2010-04-13 12:05  江南荷韵  阅读(1684)  评论(0编辑  收藏  举报