I'm working on this project for school. My part is to design the user manager. It's all in ASP.NET 2.0 and C#. Nothing I can't handle.
At work, I've been using PHP for the past 8 months or so, and it's really difficult to switch back and forth between PHP and C#. The techniques are totally completely different. Typically, Visual Studio makes things a lot easier. It's mostly just drag and drop. Except when you need to do something complex and detailed. PHP lets you right out all of the HTML by hand, so you know exactly what's going to happen and when. All of ASP's built in objects really just complicates things unless you're intimately familiar with every property and method of every function and event. I would imagine that if you were an expert in this language and knew exactly what you were doing, .Net would be a wonderfully powerful beast that would let you do all kinds of awesome things. Unfortunately, I can't unleash its full potential at my current state of expertise.
I've been fighting with the Membership provider for the past couple of weeks. The membership provider in .Net 2.0 is amazing. It lets you do all kinds of things. The API is so very nice and relatively easy to use. Except that all of .Net's asp objects are all dataset driven. And I don't know how to cram the membership data into a data set to have it display all nice.
I've been trying to shoe horn the membership data into a gridview object for the past week or so. It hasn't been working. If only I could control every little detail of ever html element like I can with PHP. Then we'd be rocking. After a ton of research I realized that the Repeater would be the way to go.
See, I can bind an ArrayList to a Repeater with no problem. The Repeater will let me have full control over what gets outputted for each element in the ArrayList. A gridview can only bind to a dataset or datasourceobject. But it's just a table. I could easily reconstruct a table using the Repeater and a bit of hand crafted HTML just as I would with a loop in PHP. Only problem is that I wouldn't have all the built in things that a gridview has (like paging and sorting).
So, I crammed all the membership data into an ArrayList and bound it to a repeater. I used the Repeater to construct a table (aka 'gridview'). I wrote about 20 billion comparison functions to sort my ArrayList. I used lots of javascript to hack in lots of conditional stuff. It's all very messy. But the important thing is that it works.
Bottom line: Instead of spending the next two months trying to figure out all the nice and easy ASP.NET objects, I just rewrote everything myself using my usual PHP techniques modified to fit into the .NET framework.
I now present to you select portions of my sourcecode:
UserList.aspx:
<br />
<table width="100%" id="userListTable">
<tr>
<th><a href="userList.aspx?sortBy=username<% if(Request.QueryString["sortBy"]== "username"){Response.Write("Rev");} %>">Email</a></th>
<th><a href="userList.aspx?sortBy=created<% if(Request.QueryString["sortBy"]== "created"){Response.Write("Rev");} %>">Created</a></th>
<th><a href="userList.aspx?sortBy=approved<% if(Request.QueryString["sortBy"]== "approved"){Response.Write("Rev");} %>">Approved</a></th>
<th><a href="userList.aspx?sortBy=student<% if(Request.QueryString["sortBy"]== "student"){Response.Write("Rev");} %>">Student</a></th>
<th><a href="userList.aspx?sortBy=faculty<% if(Request.QueryString["sortBy"]== "faculty"){Response.Write("Rev");} %>">Faculty</a></th>
<th><a href="userList.aspx?sortBy=admin<% if(Request.QueryString["sortBy"]== "admin"){Response.Write("Rev");} %>">Admin</a></th>
<th></th>
</tr>
<asp:Repeater ID="Repeater1" EnableViewState="false" runat="server">
<ItemTemplate>
<tr>
<td><%# Eval("UserName") %></td>
<td><%# Eval("CreationDate") %></td>
<td>
<input disabled id="APPROVED_<%#Eval("UserName") %>" type="checkbox" onchange="changeApproved('<%# Eval("UserName").ToString() %>')" />
<script type="text/javascript">
if("<%# System.Web.Security.Membership.GetUser(Eval("UserName").ToString()).IsApproved %>" == "True" )
document.getElementById("APPROVED_<%# Eval("UserName") %>").checked=true;
if("<%# System.Web.Security.Membership.GetUser().UserName.ToString() %>" != "<%# Eval("UserName") %>" )
document.getElementById("APPROVED_<%# Eval("UserName") %>").disabled=false;
</script>
</td>
<td>
<input id="STUDENT_<%#Eval("UserName") %>" type="checkbox" onchange="changeRole('<%# Eval("UserName").ToString() %>', 'Student')" />
<script type="text/Javascript">if("<%# System.Web.Security.Roles.IsUserInRole(Eval("UserName").ToString(), "Student") %>" == "True" ) document.write('<a href="EditStudent.aspx?userID=<%# Eval("ProviderUserKey") %>">Edit</a>')</script>
<script type="text/javascript">if("<%# System.Web.Security.Roles.IsUserInRole(Eval("UserName").ToString(), "Student") %>" == "True" ) document.getElementById("STUDENT_<%# Eval("UserName") %>").checked=true; </script>
</td>
<td>
<input id="FACULTY_<%#Eval("UserName") %>" type="checkbox" onchange="changeRole('<%# Eval("UserName").ToString() %>', 'Faculty')" />
<script type="text/Javascript">if("<%# System.Web.Security.Roles.IsUserInRole(Eval("UserName").ToString(), "Faculty") %>" == "True" ) document.write('<a href="EditFaculty.aspx?userID=<%# Eval("ProviderUserKey") %>">Edit</a>')</script>
<script type="text/javascript">if("<%# System.Web.Security.Roles.IsUserInRole(Eval("UserName").ToString(), "Faculty") %>" == "True" ) document.getElementById("FACULTY_<%# Eval("UserName") %>").checked=true; </script>
</td>
<td>
<input disabled id="ADMIN_<%#Eval("UserName") %>" type="checkbox" onchange="changeRole('<%# Eval("UserName").ToString() %>', 'Admin')" />
<script type="text/javascript">
if("<%# System.Web.Security.Roles.IsUserInRole(Eval("UserName").ToString(), "Admin") %>" == "True" )
document.getElementById("ADMIN_<%# Eval("UserName") %>").checked=true;
if("<%# System.Web.Security.Membership.GetUser().UserName.ToString() %>" != "<%# Eval("UserName") %>" )
document.getElementById("ADMIN_<%# Eval("UserName") %>").disabled=false;
</script>
</td>
<td>
<asp:LinkButton OnClientClick="return confirm('Are you sure that you want to delete this user?')" ID="LinkButton1" runat="server" CommandArgument='<%# Eval("UserName").ToString() %>' OnClick="LinkButton1_Click" Visible='<%# Membership.GetUser().UserName.ToString() != Eval("UserName").ToString() %>' >Delete</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr>
<td colspan="7">
<table style="border: 0;" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="border: 0"width="20%" align="left"><script type="text/Javascript">if(<% Response.Write( Request.QueryString["start"]);%> > 0 ) document.write('<a href="userList.aspx?sortBy=<% Response.Write( Request.QueryString["sortBy"]); %>&start=<% Response.Write( ((Request.QueryString["start"] == null)?0:Int32.Parse(Request.QueryString["start"])) - 20); %>">Previous 20</a>')</script></td>
<td style="border: 0"align="center">Page:
<script type="text/javascript">
var totalPages = <% Response.Write(Membership.GetAllUsers().Count); %> / 20;
for( var k = 0; k <= totalPages; k++)
{
document.write('<a href="userList.aspx?sortBy=<% Response.Write(Request.QueryString["sortBy"]); %>&start=' + (k*20) + '" ');
if( "<%Response.Write(Request.QueryString["start"]); %>" == ""+(k*20) || ("<%Response.Write(Request.QueryString["start"]); %>" == "" && k == 0) )
document.write(' style="font-weight: bold; text-decoration: underline"');
else
document.write(' style="font-weight: normal"');
document.write('>'+ (k+1) + '</a> ');
}
</script>
</td>
<td width="20%" style="border: 0" align="right"><script type="text/Javascript">if(<%Response.Write( Membership.GetAllUsers().Count - ((Request.QueryString["start"] == null)?0:Int32.Parse(Request.QueryString["start"]))); %> > 20 ) document.write('<a href="userList.aspx?sortBy=<% Response.Write(Request.QueryString["sortBy"]); %>&start=<%Response.Write( ((Request.QueryString["start"] == null)?0:Int32.Parse(Request.QueryString["start"])) + 20); %>">Next 20</a>')</script></td>
</tr>
</table>
</td>
</tr>
</table>
UserList.aspx.cs protected void Page_Load(object sender, EventArgs e)
{
Int16 StartItem = 0;
Int16 ItemsPerPage = 20;
String SortBy = "";
if (Request.QueryString["sortBy"] != "")
SortBy = Request.QueryString["sortBy"];
if (Request.QueryString["start"] != null)
StartItem = Int16.Parse(Request.QueryString["start"]);
ArrayList allUsers = new ArrayList(Membership.GetAllUsers()); //create an ArrayList out of the membership users collection
ArrayList pageUsers = new ArrayList();
allUsers.Reverse();
switch (SortBy) //sort the list by the desired sort method
{
case "created": allUsers.Sort(new byCreated()); break;
case "createdRev": allUsers.Sort(new byCreatedRev()); break;
case "approved": allUsers.Sort(new byApproved()); break;
case "approvedRev": allUsers.Sort(new byApprovedRev()); break;
case "student": allUsers.Sort(new byStudent()); break;
case "studentRev": allUsers.Sort(new byStudentRev()); break;
case "faculty": allUsers.Sort(new byFaculty()); break;
case "facultyRev": allUsers.Sort(new byFacultyRev()); break;
case "admin": allUsers.Sort(new byAdmin()); break;
case "adminRev": allUsers.Sort(new byAdminRev()); break;
case "usernameRev": allUsers.Sort(new byUserNameRev()); break;
case "username": allUsers.Sort(new byUserName()); break;
default: break;
}
for (int k = 0; k < ItemsPerPage; k++)
{
if(k+StartItem < allUsers.Count)
pageUsers.Add(allUsers[k + StartItem]);
}
Repeater1.DataSource = pageUsers;
Repeater1.DataBind();
}
protected void MembershipUserListDataSource_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton deleteButton = (LinkButton)sender; //typecast the sender as a link button
String Username = deleteButton.CommandArgument.ToString();
String[] usersRoles = Roles.GetRolesForUser(Username);
if(usersRoles.Length > 0)
Roles.RemoveUserFromRoles(Username, Roles.GetRolesForUser(Username));
Membership.DeleteUser(Username, true); //delete user & all related data based on deleteButton.CommandArgument
Page_Load(this, e);
}
One day, I'll learn to just do things the hard way the first time around, instead of trying to find an easier solution and wasting days more than I should.
Until next time,
~~Ben
Wisdom Archive
- August 2008 (2)
- July 2008 (2)
- June 2008 (3)
- May 2008 (3)
- April 2008 (4)
- March 2008 (7)
- February 2008 (6)
- January 2008 (1)
- December 2007 (3)
- November 2007 (9)
- October 2007 (26)
- August 2007 (1)
- June 2007 (1)
- May 2007 (1)
- March 2007 (1)
- February 2007 (2)
- December 2006 (4)
- November 2006 (4)
- October 2006 (5)
- September 2006 (2)
- August 2006 (1)
Categories
- poonheads (22)
- music (17)
- comic (16)
- site news (12)
- coding (11)
- Classic Wisdom (10)
- video (10)
- I Love Music (9)
- javascript (9)
- photo (9)
- rant (8)
- tutorial (8)
- web design (8)
- hardware (7)
- hobby (7)
- mustaches (7)
- gear (6)
- horrifying (6)
- story time (6)
- earworm (5)
- food (5)
- art (4)
- software (4)
- Coding for Complete Noobs (3)
- movie (3)
- Captain Numbskull (2)
- Everyday Wisdom (2)
- beverage (2)
- knives (2)
- review (2)
- toys (1)