用session保存购物车信息,更新数量及总价…第一次用感觉不错,帖出来分享

作者: unvs 分类: ASP.NET 发布时间: 2011-02-23 20:27 ė14,156 views 6没有评论

实现功能:用Repeater绑定购物车,实现添加商品、更改购买数量及删除选购的商品并合计出小计及总计

购物车前台代码:

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              ="color: #000000"><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>
购物车后台地址:

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( ="color: #800000">"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 &nb
sp;       
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++ yle="color: #000000">)
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    &nb
sp;    
//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"];
n>200         }
201     }
202     public string cartProductID
203     {
204         get
205         {
206             return Request.QueryString["cartProductID"];
207         }
208     }
209 }

本博文章基本上属于原创或收集整理,都是心血结晶。
欢迎转载分享,转载请注明出处,谢谢!
本文地址:用session保存购物车信息,更新数量及总价…第一次用感觉不错,帖出来分享

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Ɣ回顶部