comparison Demo/Asp.Net/Web/Controls/ShoppingCartControl.ascx.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Web.UI.WebControls;
4
5 using PetShop.ObjectModel;
6
7 namespace PetShop.Web
8 {
9 public partial class ShoppingCartControl : System.Web.UI.UserControl
10 {
11 protected void Page_PreRender(object sender, EventArgs e)
12 {
13 if (!IsPostBack)
14 BindCart();
15 }
16
17 /// <summary>
18 /// Bind repeater to Cart object in Profile
19 /// </summary>
20 private void BindCart()
21 {
22 ICollection<CartItem> cart = Profile.ShoppingCart.Items;
23
24 if (cart.Count > 0)
25 {
26 repShoppingCart.DataSource = cart;
27 repShoppingCart.DataBind();
28
29 PrintTotal();
30
31 plhTotal.Visible = true;
32 }
33 else
34 {
35 repShoppingCart.Visible = false;
36 plhTotal. Visible = false;
37 lblMsg. Text = "Your cart is empty.";
38 }
39 }
40
41 /// <summary>
42 /// Recalculate the total
43 /// </summary>
44 private void PrintTotal()
45 {
46 if (Profile.ShoppingCart.Items.Count > 0)
47 ltlTotal.Text = Profile.ShoppingCart.Total.ToString("c");
48 }
49
50 /// <summary>
51 /// Calculate total
52 /// </summary>
53 protected void BtnTotal_Click(object sender, System.Web.UI.ImageClickEventArgs e)
54 {
55 TextBox txtQuantity;
56 ImageButton btnDelete;
57 int qty = 0;
58
59 foreach (RepeaterItem row in repShoppingCart.Items)
60 {
61 txtQuantity = (TextBox) row.FindControl("txtQuantity");
62 btnDelete = (ImageButton)row.FindControl("btnDelete");
63
64 if (int.TryParse(WebUtility.InputText(txtQuantity.Text, 10), out qty))
65 {
66 if (qty > 0)
67 Profile.ShoppingCart.SetQuantity(btnDelete.CommandArgument, qty);
68 else if (qty == 0)
69 Profile.ShoppingCart.Remove(btnDelete.CommandArgument);
70 }
71 }
72
73 Profile.Save();
74 BindCart();
75 }
76
77 /// <summary>
78 /// Handler for Delete/Move buttons
79 /// </summary>
80 protected void CartItem_Command(object sender, CommandEventArgs e)
81 {
82 switch (e.CommandName.ToString())
83 {
84 case "Del":
85 Profile.ShoppingCart.Remove(e.CommandArgument.ToString());
86 break;
87
88 case "Move":
89 Profile.ShoppingCart.Remove(e.CommandArgument.ToString());
90 Profile.WishList.Add(e.CommandArgument.ToString());
91 break;
92 }
93
94 Profile.Save();
95 BindCart();
96 }
97 }
98 }