Developing and maintaining a community website can sometimes be a daunting task. A great deal of thought, and planning needs to go into creating content for your community to view, experience, and interact with. One fun piece that you will find on a number of community webistes is a "Who's Online". This is just a little bit of content that tells you how many registered users you have, how many registered users are online, how many guests are online, and the usernames of the registered users online.
So today we are going to create a usercontrol that will display who's online, in either a Horizontal or Vertical Format.
Start off with adding a usercontrol to your project with a label, and then in the codebehind we want to create a property and enum to set the orientation of our control.
Public m_Orientation As orientation
Public Enum orientation As Integer
Horizontal = 1
Vertical = 2
End Enum
Public Property Orientate() As orientation
Get
Return m_Orientation
End Get
Set(ByVal value As orientation)
m_Orientation = value
End Set
End Property
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Now the trick to this control is getting the count of our guests online. We have a couple of options to do this, one of which would be to store the session in a table and then getting the count from the table. However I didn't like going with that method. To much work. So what we are going to do is store a count in the cache each time a new session is started. And decrease that count when the session ends. So lets move over to the global.asax and set this up.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
'Initialize user count
HttpContext.Current.Cache.Insert("UserCount", 0)
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
HttpContext.Current.Cache("UserCount") += 1
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
HttpContext.Current.Cache("UserCount") -= 1
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
'Dont forget to clean up
HttpContext.Current.Cache.Remove("UserCount")
End Sub
Now heading back to our usercontrol lets do the work that builds the counts. To get the count of registered users online we call Membership.GetNumberOfUsersOnline, we then just take that count and subtract it from the "UserCount" we have stored in our cache to get the count of guests online. As you can see we are using Membership.GetAllUsers.Count to get our total registered users count and looping through GetAllUsers to get the UserName of each member online.
Private Sub GetWhosOnline()
Dim delimeter As String = "<br />"
Dim guestCount As Integer
Select Case m_Orientation
Case orientation.Horizontal
delimeter = " | "
Case orientation.Vertical
delimeter = "<br />"
End Select
If (HttpContext.Current.Cache("UserCount") IsNot Nothing) Then
guestCount = CInt(HttpContext.Current.Cache("UserCount").ToString()) - CInt(Membership.GetNumberOfUsersOnline.ToString)
End If
Me.lblWhosOnline.Text = String.Format("Total Registered: {0}{1}", Membership.GetAllUsers.Count, delimeter)
Me.lblWhosOnline.Text += String.Format("Total Online: Registered ({0}): Guests({1}){2}", _
Membership.GetNumberOfUsersOnline.ToString, guestCount, delimeter)
Me.lblWhosOnline.Text += "Who's Online: "
For Each memberUser As MembershipUser In Membership.GetAllUsers
If Membership.GetUser(memberUser.UserName).IsOnline Then
Me.lblWhosOnline.Text += memberUser.UserName & ", "
End If
Next
lblWhosOnline.Text = Left(lblWhosOnline.Text, Len(lblWhosOnline.Text) - 2)
End Sub
Now all we need to do is to include the usercontrol on a page, and set the our property for how we want it displayed.
<uc1:ucWhosOnline id="UcWhosOnline1" runat="server" Orientate="Vertical" />
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
And now you have a "Who's Online" usercontrol for your community website.
Enjoy,
Dave
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Technorati Tags:
.NET,
VB.Net
Read the complete post at http://feeds.feedburner.com/~r/DavidYancey/~3/231810400/create-a-who-s-online-using-net-s-membership-provider.aspx