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