-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomePage.aspx.cs
89 lines (75 loc) · 3.21 KB
/
HomePage.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
using CSharpAssignment.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CSharpAssignment
{
public partial class HomePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
void AddToShoppingCart(string itemID, string itemName, string itemPrice)
{
DataTable dataTable;
// If this object does not already exist, initialize the DataTable object and
// then add the information of the selected product to the DataTable object
if (Session["ShoppingCart"] == null)
{
dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Quantity");
dataTable.Columns.Add("UnitPrice");
dataTable.Columns.Add("Price");
}
// If the Shopping Cart object already exists, you declare to convert the data
// type of this object into the DataTable object
else
{
dataTable = (DataTable)Session["ShoppingCart"];
}
// Check whether the product already exists in ShoppingCart
Common cls = new Common();
int indexOfItem = cls.IsExistItemInShoppingCart(itemID, dataTable);
// If there are already, update add 1
if (indexOfItem != -1)
{
dataTable.Rows[indexOfItem]["Quantity"] = Convert.ToInt32(dataTable.Rows[indexOfItem]["Quantity"]) + 1;
dataTable.Rows[indexOfItem]["Price"] = Convert.ToDouble(dataTable.Rows[indexOfItem]["Price"]) + Convert.ToDouble(dataTable.Rows[indexOfItem]["Price"]);
}
// If not, add a new product to Shopping Cart
else
{
DataRow dataRow = dataTable.NewRow();
dataRow["ID"] = itemID;
dataRow["Name"] = itemName;
dataRow["Quantity"] = "1";
dataRow["Price"] = itemPrice;
dataRow["UnitPrice"] = itemPrice;
dataTable.Rows.Add(dataRow);
}
// Assign the DataTable object to the Session object
Session["ShoppingCart"] = dataTable;
}
protected void DLProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
}
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
Button btnAddToCart = (Button)sender;
DataListItem dataListItem = (DataListItem)btnAddToCart.Parent;
HiddenField hidden = (HiddenField)dataListItem.FindControl("IDLabel");
Label NameLabel1 = (Label)dataListItem.FindControl("NameLabel1");
Label PriceLabel1 = (Label)dataListItem.FindControl("PriceLabel1");
//Literal1.Text = "You just added \"" + labelName.Text + "\" to Shopping cart";
// Call the method to add to the shopping cart
AddToShoppingCart(hidden.Value, NameLabel1.Text, PriceLabel1.Text);
}
}
}