-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchUsers.aspx.cs
229 lines (180 loc) · 6.79 KB
/
searchUsers.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
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Microsoft.AspNet.Membership;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Web.Security;
using System.Collections;
using System.Web.UI.HtmlControls;
public partial class searchUsers : System.Web.UI.Page
{
String usernameInSearchBox = "";
String userId = "";
protected List<student22> studentList;
protected static string tempUserID = "";
private static bool areFriends;
friendHandler friendHld;
String currentlyLoggedUserID;
String currentlyLoggedUserName;
protected void Page_Load(object sender, EventArgs e)
{
if (!(HttpContext.Current.User.Identity.IsAuthenticated))
{
FormsAuthentication.SignOut();
Session.Clear();
Response.Redirect("~/Default");
}
currentlyLoggedUserName = HttpContext.Current.User.Identity.Name;
currentlyLoggedUserID = Membership.GetUser(currentlyLoggedUserName).ProviderUserKey.ToString();
if (currentlyLoggedUserID == null)
{
Response.Redirect("~/Default");
}
studentList = new List<student22>();
if (!IsPostBack)
seachAllUSers.Visible = false;
userFoundLabel.Visible = false;
usernameInSearchBox = searchBox.Text;
friendHld = new friendHandler();
//check to see if they are friends or not
createTable();
}//end of searchUsers class
private bool areTheyFriends(String visitedUserId)
{
areFriends = friendHld.checkIfFriend(currentlyLoggedUserID, visitedUserId);
return areFriends;
}
void btn_Click(object sender, EventArgs e)
{
string name = ((sender) as Button).ID;
//send this username to the viewProfile page
// string name = usernameInSearchBox;
Session["name"] = name;
Response.Redirect("viewProfile.aspx");
}
void btnAddFriend_Click(object sender, EventArgs e)
{
string friendId = ((sender) as Button).ID;
bool aretheyfriends = areTheyFriends(friendId);
if (aretheyfriends)
{
friendHld.removeFriend(currentlyLoggedUserID, friendId);
((sender) as Button).Text = "Add Friend";
}
else
{
friendHld.addFriend(currentlyLoggedUserID, friendId);
((sender) as Button).Text = "Unfriend";
}
}
private void createTable()
{
UserHandler myUserHld = new UserHandler();
List<student22> studentList = myUserHld.GetAllStudentsList();
HtmlTable myTable = new HtmlTable();
myTable.Attributes["class"] = "table";
foreach (var student in studentList)
{
HtmlTableRow row = new HtmlTableRow();
row.Attributes["class"] = "";
HtmlTableCell cell1 = new HtmlTableCell();
cell1.Attributes["class"] = "";
var userNameLabel = new Label();
userNameLabel.Text = student.getUsername();
cell1.Controls.Add(userNameLabel);
//cell1.InnerText = student.getUsername();
row.Controls.Add(cell1);
HtmlTableCell cell2 = new HtmlTableCell();
Image img = new Image();
img.Attributes["class"] = "media-object";
img.Height = 100;
img.Width = 100;
img.AlternateText = "No image on file";
img.ImageUrl = "ImageHandler.ashx?UserId=" + student.getStudent_id();
//add the btn to cell3
cell2.Controls.Add(img);
//add cell3 to the row
row.Controls.Add(cell2);
HtmlTableCell cell3 = new HtmlTableCell();
//create button
Button btn = new Button();
btn.Attributes["class"] = "btn btn-success";
btn.Text = "view User Profile";
btn.ID = student.getUsername();
//event hadler for button
btn.Click += new EventHandler(btn_Click);
//add the btn to cell3
cell3.Controls.Add(btn);
//add cell3 to the row
row.Controls.Add(cell3);
if (!(student.getStudent_id().Equals(currentlyLoggedUserID)))
{
HtmlTableCell cell4 = new HtmlTableCell();
//create button
Button btnAddFriend = new Button();
btnAddFriend.Attributes["class"] = "btn btn-primary";
bool aretheyfriends = areTheyFriends(student.getStudent_id());
if (aretheyfriends)
{
btnAddFriend.Text = "Unfriend";
}
else
{
btnAddFriend.Text = "Add Friend";
}
btnAddFriend.ID = student.getStudent_id();
//event hadler for button
btnAddFriend.Click += new EventHandler(btnAddFriend_Click);
//add the btn to cell3
cell4.Controls.Add(btnAddFriend);
//add cell3 to the row
row.Controls.Add(cell4);
}
//add all rows to table
myTable.Controls.Add(row);
}
PlaceHolder1.Controls.Add(myTable);
}
public void search_Click(object sender, EventArgs e)
{
//if the user enters anything in the search box
if (!(String.IsNullOrEmpty(usernameInSearchBox)))
{
//check the entered text to see if it exist in the users table
storedProcedure myProc = new storedProcedure("checkUserName");
paramter param1 = new paramter("@theUsername", usernameInSearchBox);
myProc.addParam(param1);
//if that username exist the userID will be returned
userId = myProc.executeScalarParam();
//if the userId is not null or empty means the user was found
if (!(String.IsNullOrEmpty(userId)))
{
userFoundLabel.Text = "user " + usernameInSearchBox + " found";
userFoundLabel.Visible = true;
//send this username to the viewProfile page
string name = usernameInSearchBox;
Session["name"] = name;
Response.Redirect("viewProfile.aspx");
}
else
{
userFoundLabel.Text = "" + usernameInSearchBox + " is not a valid username";
userFoundLabel.Visible = true;
}
}
else
{
userFoundLabel.Text = "Enter a username";
userFoundLabel.Visible = true;
}
}//end of search_Click
public void allUSers_Click(object sender, EventArgs e)
{
seachAllUSers.Visible = true;
}
}