-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminProductDetails.aspx.cs
224 lines (206 loc) · 8.22 KB
/
AdminProductDetails.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class AdminProductDetails : System.Web.UI.Page
{
// store product, category and department IDs as class members
private string currentProductId, currentCategoryId, currentDepartmentId;
protected void Page_Load(object sender, EventArgs e)
{
// Get DepartmentID, CategoryID, ProductID from the query string
// and save their values
currentDepartmentId = Request.QueryString["DepartmentID"];
currentCategoryId = Request.QueryString["CategoryID"];
currentProductId = Request.QueryString["ProductID"];
// Fill the controls with data only on the initial page load
if (!IsPostBack)
{
// Fill controls with data
PopulateControls();
}
}
// Populate the controls
private void PopulateControls()
{
// Retrieve product details and category details from database
ProductDetails productDetails = CatalogAccess.GetProductDetails(currentProductId);
CategoryDetails categoryDetails = CatalogAccess.GetCategoryDetails(currentCategoryId);
// Set up labels and images
productNameLabel.Text = productDetails.Name;
image1.ImageUrl = Link.ToProductImage(productDetails.Thumbnail);
image2.ImageUrl = Link.ToProductImage(productDetails.Image);
// Link to department
catLink.Text = categoryDetails.Name;
catLink.NavigateUrl = "AdminCategories.aspx?DepartmentID=" + currentDepartmentId;
// Clear form
categoriesLabel.Text = "";
categoriesListAssign.Items.Clear();
categoriesListMove.Items.Clear();
categoriesListRemove.Items.Clear();
// Fill categoriesLabel and categoriesListRemove with data
string categoryId, categoryName;
DataTable productCategories = CatalogAccess.GetCategoriesWithProduct(currentProductId);
for (int i = 0; i < productCategories.Rows.Count; i++)
{
// obtain category id and name
categoryId = productCategories.Rows[i]["CategoryId"].ToString();
categoryName = productCategories.Rows[i]["Name"].ToString();
// add a link to the category admin page
categoriesLabel.Text +=
(categoriesLabel.Text == "" ? "" : ", ") +
"<a href='AdminProducts.aspx?DepartmentID=" +
CatalogAccess.GetCategoryDetails(currentCategoryId).DepartmentId +
"&CategoryID=" + categoryId + "'>" +
categoryName + "</a>";
// populate the categoriesListRemove combo box
categoriesListRemove.Items.Add(new ListItem(categoryName, categoryId));
}
// Delete from catalog or remove from category?
if (productCategories.Rows.Count > 1)
{
deleteButton.Visible = false;
removeButton.Enabled = true;
}
else
{
deleteButton.Visible = true;
removeButton.Enabled = false;
}
// Fill categoriesListMove and categoriesListAssign with data
productCategories = CatalogAccess.GetCategoriesWithoutProduct(currentProductId);
for (int i = 0; i < productCategories.Rows.Count; i++)
{
// obtain category id and name
categoryId = productCategories.Rows[i]["CategoryId"].ToString();
categoryName = productCategories.Rows[i]["Name"].ToString();
// populate the list boxes
categoriesListAssign.Items.Add(new ListItem(categoryName, categoryId));
categoriesListMove.Items.Add(new ListItem(categoryName, categoryId));
}
}
// Remove the product from a category
protected void removeButton_Click(object sender, EventArgs e)
{
// Check if a category was selected
if (categoriesListRemove.SelectedIndex != -1)
{
// Get the category ID that was selected in the DropDownList
string categoryId = categoriesListRemove.SelectedItem.Value;
// Remove the product from the category
bool success = CatalogAccess.RemoveProductFromCategory(currentProductId, categoryId);
// Display status message
statusLabel.Text = success ? "Product removed successfully" : "Product removal failed";
// Refresh the page
PopulateControls();
}
else
statusLabel.Text = "You need to select a category";
}
// delete a product from the catalog
protected void deleteButton_Click(object sender, EventArgs e)
{
// Delete the product from the catalog
CatalogAccess.DeleteProduct(currentProductId);
// Need to go back to the categories page now
Response.Redirect("AdminDepartments.aspx");
}
// assign the product to a new category
protected void assignButton_Click(object sender, EventArgs e)
{
// Check if a category was selected
if (categoriesListAssign.SelectedIndex != -1)
{
// Get the category ID that was selected in the DropDownList
string categoryId = categoriesListAssign.SelectedItem.Value;
// Assign the product to the category
bool success = CatalogAccess.AssignProductToCategory(currentProductId, categoryId);
// Display status message
statusLabel.Text = success ? "Product assigned successfully" : "Product assignation failed";
// Refresh the page
PopulateControls();
}
else
statusLabel.Text = "You need to select a category";
}
// move the product to another category
protected void moveButton_Click(object sender, EventArgs e)
{
// Check if a category was selected
if (categoriesListMove.SelectedIndex != -1)
{
// Get the category ID that was selected in the DropDownList
string newCategoryId = categoriesListMove.SelectedItem.Value;
// Move the product to the category
bool success = CatalogAccess.MoveProductToCategory(currentProductId, currentCategoryId, newCategoryId);
// If the operation was successful, reload the page,
// so the new category will reflect in the query string
if (!success)
statusLabel.Text = "Couldn't move the product to the specified category";
else
Response.Redirect("AdminProductDetails.aspx" +
"?DepartmentID=" + currentDepartmentId +
"&CategoryID=" + newCategoryId +
"&ProductID=" + currentProductId);
}
else
statusLabel.Text = "You need to select a category";
}
// upload product's first image
protected void upload1Button_Click(object sender, EventArgs e)
{
// proceed with uploading only if the user selected a file
if (image1FileUpload.HasFile)
{
try
{
string fileName = image1FileUpload.FileName;
string location = Server.MapPath("./ProductImages/") + fileName;
// save image to server
image1FileUpload.SaveAs(location);
// update database with new product details
ProductDetails pd = CatalogAccess.GetProductDetails(currentProductId);
CatalogAccess.UpdateProduct(currentProductId, pd.Name, pd.Description, pd.Price.ToString(), fileName, pd.Image, pd.PromoDept.ToString(), pd.PromoFront.ToString());
// reload the page
Response.Redirect("AdminProductDetails.aspx" +
"?DepartmentID=" + currentDepartmentId +
"&CategoryID=" + currentCategoryId +
"&ProductID=" + currentProductId);
}
catch
{
statusLabel.Text = "Uploading image 1 failed";
}
}
}
// upload product's second image
protected void upload2Button_Click(object sender, EventArgs e)
{
// proceed with uploading only if the user selected a file
if (image2FileUpload.HasFile)
{
try
{
string fileName = image2FileUpload.FileName;
string location = Server.MapPath("./ProductImages/") + fileName;
// save image to server
image2FileUpload.SaveAs(location);
// update database with new product details
ProductDetails pd = CatalogAccess.GetProductDetails(currentProductId);
CatalogAccess.UpdateProduct(currentProductId, pd.Name, pd.Description, pd.Price.ToString(), pd.Thumbnail, fileName,pd.PromoDept.ToString(), pd.PromoFront.ToString());
// reload the page
Response.Redirect("AdminProductDetails.aspx" +
"?DepartmentID=" + currentDepartmentId +
"&CategoryID=" + currentCategoryId +
"&ProductID=" + currentProductId);
}
catch
{
statusLabel.Text = "Uploading image 2 failed";
}
}
}
}