-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgramme.aspx.cs
More file actions
544 lines (453 loc) · 18.8 KB
/
Copy pathProgramme.aspx.cs
File metadata and controls
544 lines (453 loc) · 18.8 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UniFinder
{
public partial class Programme : System.Web.UI.Page
{
private int CurrentPage
{
get { return (int)(ViewState["CurrentPage"] ?? 1); }
set { ViewState["CurrentPage"] = value; }
}
//private int PageSize
//{
// get { return int.Parse(ddlPageSize.SelectedValue); }
//}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ResetFilters();
BindUniversities(); // Method to bind universities from the database
BindPrograms();
if (Session["Wishlist"] == null)
{
Session["Wishlist"] = new List<int>();
}
UpdateWishlistLabel();
UpdatePageNumberLabel();
// Register the event handler
ddlSortBy.SelectedIndexChanged += new EventHandler(ddlSortBy_SelectedIndexChanged);
}
RestoreCompareState();
}
private void BindUniversities()
{
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
string query = "SELECT DISTINCT u.uniID, u.uniNameEng FROM University u " +
"INNER JOIN Programme p on u.uniID = p.uniID";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
// Add an "All Universities" option at the top
DataRow allUniversitiesRow = dt.NewRow();
allUniversitiesRow["uniID"] = "All"; // Unique identifier for "All"
allUniversitiesRow["uniNameEng"] = "All Universities";
dt.Rows.InsertAt(allUniversitiesRow, 0); // Insert at the top
ddlUni.DataSource = dt;
ddlUni.DataTextField = "uniNameEng";
ddlUni.DataValueField = "uniID";
ddlUni.DataBind();
}
}
private void BindBranches()
{
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
string query = "SELECT DISTINCT b.branchID, b.location FROM Branch b " +
"INNER JOIN Programme p ON b.branchID = p.branchID " +
"WHERE b.uniID = @uniID ORDER BY b.location";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@uniID", ddlUni.SelectedValue);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
ddlBranch.Items.Clear();
ddlBranch.Items.Add(new ListItem("All Branches", "All")); // Ensure "All Branches" is added
while (dr.Read())
{
string branchID = dr["branchID"].ToString();
string location = dr["location"].ToString();
ddlBranch.Items.Add(new ListItem(location, branchID));
}
dr.Close();
conn.Close();
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
if (CurrentPage > 1)
{
CurrentPage--;
BindPrograms();
UpdatePageNumberLabel();
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
CurrentPage++;
BindPrograms();
UpdatePageNumberLabel();
}
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentPage = 1;
BindPrograms();
UpdatePageNumberLabel();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
CurrentPage = 1;
BindPrograms();
UpdatePageNumberLabel();
}
protected void btnReset_Click(object sender, EventArgs e)
{
ResetFilters();
}
protected void ResetFilters()
{
txtSearch.Text = string.Empty;
ddlUni.SelectedIndex = 0; // Default to "All Universities"
ddlBranch.Items.Clear(); // Clear branch dropdown items
ddlBranch.Items.Add(new ListItem("All Branches", "All")); // Add "All Branches" option
ddlBranch.SelectedIndex = 0; // Set default value
ddlSortBy.SelectedIndex = 0; // Default to first sorting option
txtMinFees.Text = string.Empty;
txtMaxFees.Text = string.Empty;
txtDuration.Text = string.Empty;
// Reset pagination to the first page
CurrentPage = 1;
// Rebind the universities and branches
BindUniversities();
BindBranches(); // Ensure this method gets the "All Branches" option
// Bind programs with default settings (i.e., no filters)
BindPrograms();
// Update labels and any other UI elements
UpdateWishlistLabel();
UpdatePageNumberLabel();
}
private const int pageSize = 12; // Ensure this is set to 12
PagedDataSource pagedData = new PagedDataSource();
private void BindPrograms()
{
string searchQuery = txtSearch.Text.Trim();
string universityFilter = ddlUni.SelectedValue;
string branchFilter = ddlBranch.SelectedValue;
string minFees = txtMinFees.Text.Trim();
string maxFees = txtMaxFees.Text.Trim();
string duration = txtDuration.Text.Trim();
string sortOrder = ddlSortBy.SelectedValue;
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
string query = @"
SELECT DISTINCT p.programID, p.programName AS ProgrammeName, u.uniNameEng AS UniversityName, u.uniLogo, p.fees, p.duration, b.location
FROM Programme p
JOIN University u ON p.uniID = u.uniID
LEFT JOIN Branch b ON p.branchID = b.branchID
WHERE 1=1"; // Ensures the query is always valid
// Add filters based on user selection
if (!string.IsNullOrEmpty(searchQuery))
{
query += " AND p.programName LIKE @SearchQuery";
}
if (universityFilter != "All") // Filter by university
{
query += " AND u.uniID = @UniversityFilter";
}
if (branchFilter != "All") // Filter by branch
{
query += " AND p.branchID = @BranchFilter";
}
if (!string.IsNullOrEmpty(minFees))
{
query += " AND p.fees >= @MinFees";
}
if (!string.IsNullOrEmpty(maxFees))
{
query += " AND p.fees <= @MaxFees";
}
if (!string.IsNullOrEmpty(duration))
{
query += " AND p.duration = @Duration";
}
// Sorting
switch (sortOrder)
{
case "fees_desc":
query += " ORDER BY p.fees DESC";
break;
case "fees_asc":
query += " ORDER BY p.fees ASC";
break;
default:
query += " ORDER BY p.programName ASC";
break;
}
SqlCommand cmd = new SqlCommand(query, conn);
// Add parameters only if their values are provided
if (!string.IsNullOrEmpty(searchQuery))
{
cmd.Parameters.AddWithValue("@SearchQuery", "%" + searchQuery + "%");
}
if (universityFilter != "All")
{
cmd.Parameters.AddWithValue("@UniversityFilter", universityFilter);
}
if (branchFilter != "All")
{
cmd.Parameters.AddWithValue("@BranchFilter", branchFilter);
}
if (!string.IsNullOrEmpty(minFees))
{
cmd.Parameters.AddWithValue("@MinFees", minFees);
}
if (!string.IsNullOrEmpty(maxFees))
{
cmd.Parameters.AddWithValue("@MaxFees", maxFees);
}
if (!string.IsNullOrEmpty(duration))
{
cmd.Parameters.AddWithValue("@Duration", duration);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 0)
{
lblNoPrograms.Visible = true; // Show no results message
DataList1.Visible = false; // Hide DataList
}
else
{
lblNoPrograms.Visible = false; // Hide no results message
DataList1.Visible = true; // Show DataList
// Implement pagination
pagedData.DataSource = dt.DefaultView;
pagedData.AllowPaging = true;
pagedData.PageSize = pageSize;
pagedData.CurrentPageIndex = CurrentPage - 1;
DataList1.DataSource = pagedData;
DataList1.DataBind();
// Update page navigation controls
UpdatePageNumberLabel();
}
RestoreCompareState();
}
}
protected void ddlSortBy_SelectedIndexChanged(object sender, EventArgs e)
{
BindPrograms();
UpdatePageNumberLabel();
}
private void UpdatePageNumberLabel()
{
lblPageNumber.Text = $"Page {CurrentPage} of {pagedData.PageCount}";
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "AddToWishlist")
{
int programmeId = Convert.ToInt32(e.CommandArgument);
var wishlist = Session["Wishlist"] as List<int>;
if (wishlist == null)
{
wishlist = new List<int>();
Session["Wishlist"] = wishlist;
}
if (wishlist.Count < 4)
{
if (!wishlist.Contains(programmeId))
{
wishlist.Add(programmeId);
}
}
UpdateWishlistLabel();
}
}
private void UpdateWishlistLabel()
{
var wishlist = Session["Wishlist"] as List<int>;
var wishlistNames = new List<string>();
if (wishlist != null && wishlist.Count > 0)
{
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
string query = @"
SELECT p.programName
FROM Programme p
WHERE p.programID IN (" + string.Join(",", wishlist) + ")";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
wishlistNames.Add(row["programName"].ToString());
}
}
}
else
{
}
}
protected void imgBtnSelectProgram(object sender, ImageClickEventArgs e)
{
ImageButton btn = sender as ImageButton;
if (btn != null)
{
string programNameChosen = btn.AlternateText;
Response.Redirect($"ProgrammeDetail.aspx?programName={HttpUtility.UrlEncode(programNameChosen)}");
}
}
protected string GetImageUrl(object uniLogo)
{
if (uniLogo != DBNull.Value && uniLogo != null)
{
byte[] bytes = (byte[])uniLogo;
return "data:image/png;base64," + Convert.ToBase64String(bytes);
}
else
{
return ResolveUrl("~/Images/LogoNew.png");
}
}
protected void AddToCompareButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Text = "Added to Compare";
btn.CssClass += " greyed-out";
btn.Enabled = false;
string programId = btn.CommandArgument;
List<string> compareList = (List<string>)Session["CompareList"] ?? new List<string>();
if (compareList.Count >= 4)
{
lblErrorMessage.Text = "Only up to 4 universities can be added to wishlist.";
return;
}
if (!compareList.Contains(programId))
{
compareList.Add(programId);
Session["CompareList"] = compareList;
btn.Text = "Added to Compare";
btn.Enabled = false;
// Update hidden field to store compare list
compareListHiddenField.Value = string.Join(",", compareList);
}
}
private void RestoreCompareState()
{
List<string> compareList = (List<string>)Session["CompareList"];
if (compareList != null)
{
foreach (DataListItem item in DataList1.Items)
{
Button btn = (Button)item.FindControl("btnAddToCompare");
string programId = btn.CommandArgument;
if (compareList.Contains(programId))
{
btn.Text = "Added to Compare";
btn.CssClass += " greyed-out";
btn.Enabled = false;
}
}
// Update hidden field to store compare list
compareListHiddenField.Value = string.Join(",", compareList);
}
}
protected void CompareButton_Click(object sender, EventArgs e)
{
// Ensure compare list is up-to-date
List<string> compareList = new List<string>(compareListHiddenField.Value.Split(','));
Session["CompareList"] = compareList;
// Redirect to Comparison.aspx
Response.Redirect("~/MyAccount/Wishlist.aspx");
}
private DataRow GetProgramDetailsById(string programId)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(
"SELECT u.uniNameEng AS University, p.programName AS ProgrammeName, b.location AS Location, p.fees AS Fees, p.duration AS Duration " +
"FROM Programme p " +
"JOIN University u ON p.uniID = u.uniID " +
"JOIN Branch b ON u.uniID = b.uniID " +
"WHERE p.programID = @programID", con))
{
cmd.Parameters.AddWithValue("@programID", programId);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}
if (dt.Rows.Count > 0)
{
return dt.Rows[0];
}
else
{
return null;
}
}
protected void ddlUni_SelectedIndexChanged1(object sender, EventArgs e)
{
if (ddlUni.SelectedValue == "All") // Assuming "All" represents "All Universities"
{
ddlBranch.Enabled = false; // Disable branch dropdown
ddlBranch.SelectedIndex = 0; // Optionally reset the branch dropdown
}
else
{
ddlBranch.Enabled = true; // Enable branch dropdown
//BindBranchesForSelectedUniversity(); // Bind branches for the selected university
BindBranches(); // Ensure this method populates ddlBranch based on the selected university
}
// Bind programs based on the updated dropdown states
CurrentPage = 1;
BindPrograms();
UpdatePageNumberLabel();
}
protected void ddlUni_SelectedIndexChanged(object sender, EventArgs e)
{
BindBranches(); // Ensure this method populates ddlBranch based on the selected university
BindPrograms(); // Optionally refresh programs if needed
}
private void BindBranchesForSelectedUniversity()
{
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
string universityID = ddlUni.SelectedValue;
string query = "SELECT DISTINCT b.branchID, b.location " +
"FROM Branch b " +
"JOIN Programme p ON b.branchID = p.branchID " +
"WHERE p.uniID = @UniversityID " +
"ORDER BY b.location";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@UniversityID", universityID);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
ddlBranch.Items.Clear();
ddlBranch.Items.Add(new ListItem("All Branches", "0")); // Ensure "All Branches" is added
while (dr.Read())
{
string branchID = dr["branchID"].ToString();
string location = dr["location"].ToString();
ddlBranch.Items.Add(new ListItem(location, branchID));
}
dr.Close();
conn.Close();
}
}
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}