-
Notifications
You must be signed in to change notification settings - Fork 0
/
userMaintenance.aspx.cs
175 lines (163 loc) · 5.23 KB
/
userMaintenance.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class userMaintenance : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] == null)
{
Response.Redirect("home.aspx");
}
if (!IsPostBack)
{
if (Request.QueryString["src"] != null)
{
if (Request.QueryString["src"].ToString() == "edit")
{
LoadUser(Convert.ToInt32(Request.QueryString["userID"].ToString()));
}
}
else
{
LoadNewForm();
}
}
}
protected void LoadUser(int userID)
{
lblHeader.Text = "Edit User";
lblUserID.Visible = true;
lblUserIDNo.Visible = true;
lblUserIDNo.Text = userID.ToString();
pnlEditUser.Visible = true;
hylDelete.Visible = true;
btnSaveUpdate.Visible = true;
pnlPasswordInfo.Visible = true;
DataTable results = UserDB.GetUser(userID);
if (results.Rows.Count == 1)
{
lblUsernameValue.Text = results.Rows[0]["Username"].ToString();
ddlUserType.SelectedIndex = Convert.ToInt32(results.Rows[0]["UserTypeID"]);
if (results.Rows[0]["Username"].ToString().Equals(Session["Username"].ToString()))
{
hylDelete.Visible = false;
}
}
}
protected void LoadNewForm()
{
lblHeader.Text = "Add New User";
pnlNewUser.Visible = true;
rfvUsername.Enabled = true;
rfvPassword1.Enabled = true;
rfvPassword2.Enabled = true;
btnSaveNew.Visible = true;
}
protected void btnSaveUpdate_Click(object sender, EventArgs e)
{
//If both password fields blank
if (txtPassword1.Text == "" && txtPassword2.Text == "")
{
//Update user without password
if (UserDB.UpdateUser(Convert.ToInt32(lblUserIDNo.Text), ddlUserType.SelectedIndex))
{
//If update returns true, return to user list
Response.Redirect("userList.aspx?src=update");
}
else
{
//If update fails display error alert
litMessage.Text =
"<div class='alert alert-error'>" +
"<button type='button' class='close' data-dismiss='alert'>×</button>" +
"Update failed." +
"</div>";
}
}
//Else if password1 = password2 and both are not blank
else if (txtPassword1.Text.Equals(txtPassword2.Text) && txtPassword1.Text != "")
{
//Update user with password
if (UserDB.UpdateUser(Convert.ToInt32(lblUserIDNo.Text), ddlUserType.SelectedIndex))
{
//If update returns true, return to user list
Response.Redirect("userList.aspx?src=update");
}
else
{
//If update fails display error alert
litMessage.Text =
"<div class='alert alert-error'>" +
"<button type='button' class='close' data-dismiss='alert'>×</button>" +
"Update failed." +
"</div>";
}
}
//Else if password1 does not equal password2
else if (!txtPassword1.Text.Equals(txtPassword2.Text))
{
//Display does not match label
lblPasswordMatch.Visible = true;
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("userlist.aspx");
}
protected void btnDelete_Click(object sender, EventArgs e)
{
if (UserDB.DeleteUser(Convert.ToInt32(lblUserIDNo.Text)))
{
Response.Redirect("userlist.aspx?src=delete");
}
else
{
Response.Redirect(Request.Url + "&fail=delete");
}
}
protected void btnSaveNew_Click(object sender, EventArgs e)
{
//Check if username already used
if (UserDB.CheckForUsername(txtUsername.Text))
{
//Username already used
litMessage.Text =
"<div class='alert alert-error'>" +
"<button type='button' class='close' data-dismiss='alert'>×</button>" +
"Username is already used. Please try another." +
"</div>";
litMessage.Visible = true;
}
//Else if password1 = password2 and both are not blank
else if (txtPassword1.Text.Equals(txtPassword2.Text) && txtPassword1.Text != "")
{
//Update user with password
if (UserDB.AddUser(txtUsername.Text, ddlUserType.SelectedIndex, txtPassword1.Text) != null)
{
//If insert returns ID, return to user list
Response.Redirect("userList.aspx?src=added");
}
else
{
//If update fails display error alert
litMessage.Text =
"<div class='alert alert-error'>" +
"<button type='button' class='close' data-dismiss='alert'>×</button>" +
"Create user failed." +
"</div>";
}
}
//Else if password1 does not equal password2
else if (!txtPassword1.Text.Equals(txtPassword2.Text))
{
//Display does not match label
lblPasswordMatch.Visible = true;
}
}
}