-
Notifications
You must be signed in to change notification settings - Fork 0
/
Addtotable.aspx.cs
145 lines (123 loc) · 4.81 KB
/
Addtotable.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ApplicationState
{
public partial class CartPage : Page
{
private static int productCounter = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Application["CartTable"] == null)
{
Application["CartTable"] = CreateCartTable();
}
BindCartTable();
}
}
private DataTable CreateCartTable()
{
var table = new DataTable();
table.Columns.Add("ProductNumber", typeof(int));
table.Columns.Add("ProductName", typeof(string));
table.Columns.Add("Quantity", typeof(int));
table.Columns.Add("Price", typeof(decimal));
table.Columns.Add("SubTotal", typeof(decimal));
table.Columns.Add("Total", typeof(decimal)); // Add Total column to match your UI
return table;
}
protected void BindCartTable()
{
var cartTable = (DataTable)Application["CartTable"];
UpdateTotals(cartTable);
Repeater.DataSource = cartTable;
Repeater.DataBind();
UpdateOverallTotal(cartTable);
// Trigger partial postbacks for UpdatePanels
UpdatePanel1.Update();
UpdatePanelTotal.Update();
}
private void UpdateTotals(DataTable cartTable)
{
foreach (DataRow row in cartTable.Rows)
{
// Ensure Total mirrors SubTotal or includes additional logic if needed
row["Total"] = row.Field<decimal>("SubTotal");
}
}
private void UpdateOverallTotal(DataTable cartTable)
{
decimal total = cartTable.AsEnumerable().Sum(row => row.Field<decimal>("SubTotal"));
lblTotal.Text = "Php " + total.ToString("N2");
}
protected void AddToCart_Click(object sender, CommandEventArgs e)
{
var args = e.CommandArgument.ToString().Split(',');
string productName = args[0];
decimal price = Convert.ToDecimal(args[1]);
var cartTable = (DataTable)Application["CartTable"];
var row = cartTable.AsEnumerable().FirstOrDefault(r => r.Field<string>("ProductName") == productName);
if (row != null)
{
int currentQuantity = Convert.ToInt32(row["Quantity"]);
row["Quantity"] = currentQuantity + 1;
row["SubTotal"] = (currentQuantity + 1) * price;
}
else
{
cartTable.Rows.Add(productCounter++, productName, 1, price, price, price);
}
Application["CartTable"] = cartTable;
BindCartTable(); // Refresh the cart and total
}
protected void UpdateQuantity_Click(object sender, CommandEventArgs e)
{
var args = e.CommandArgument.ToString().Split(',');
int productNumber = Convert.ToInt32(args[0]);
int change = Convert.ToInt32(args[1]);
var cartTable = (DataTable)Application["CartTable"];
var row = cartTable.AsEnumerable().FirstOrDefault(r => r.Field<int>("ProductNumber") == productNumber);
if (row != null)
{
int newQuantity = row.Field<int>("Quantity") + change;
if (newQuantity <= 0)
{
cartTable.Rows.Remove(row);
}
else
{
row["Quantity"] = newQuantity;
row["SubTotal"] = newQuantity * row.Field<decimal>("Price");
}
}
Application["CartTable"] = cartTable;
BindCartTable(); // Refresh the cart and total
}
protected void DeleteProduct_Click(object sender, CommandEventArgs e)
{
int productNumber = Convert.ToInt32(e.CommandArgument);
var cartTable = (DataTable)Application["CartTable"];
var row = cartTable.AsEnumerable().FirstOrDefault(r => r.Field<int>("ProductNumber") == productNumber);
if (row != null)
{
cartTable.Rows.Remove(row);
}
Application["CartTable"] = cartTable; // Save changes back to Application state
BindCartTable();
if (cartTable.Rows.Count == 0)
{
lblTotal.Text = "Php 0.00";
}
}
protected void Clear_Click(object sender, EventArgs e)
{
Application["CartTable"] = CreateCartTable();
BindCartTable();
lblTotal.Text = "Php 0.00"; // Reset total to zero after clearing
}
}
}