Tutorials » AspNet tutorials »

Display currently online users


This chapter explains how you can extend your UserTracker project to display the list of currently online users.


In the previous chapter, you created a login page and added some functionality to it. When a user enter his name and submit the page, the name is stored in session variable and also an ArrayList. The arraylist is stored into an Application variable.

In this chapter, we will create another page, which will retrieve all names from the arraylist in the Application variable and display it.

Create a new page



Let us create a new page called "ShowNames.aspx". Right click on the project name in the solution explorer and create this new page.

Copy and paste the following code in to the aspx page, in the HTML mode:


<B>All Users</B>
<hr>
<asp:Label id="lblNames" runat="server"></asp:Label>


Now switch to the design mode and make sure the controls appear properly.

What we have done is, display a static title ("All Users") and put a horizontal line below the title. Below that, we have added a label control named "lblNames".

Now, in the code behind file, we will retrieve all names from the arraylist in application variable and display it in the label.

Double click on the form in design mode. It will create a Page_Load event handler. Replace the Page_Load event handler with the following code:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim names As ArrayList = Application("Names")

If names Is Nothing OrElse names.Count = 0 Then
lblNames.Text = "No names are added to application variable."
Else
Dim name As String

Append each of the names from the arraylist to the label.
For Each name In names
lblNames.Text = lblNames.Text & name & "
"
Next
End If
End Sub


Let us analyze the above code. The statement Dim names As ArrayList = Application("Names")
declares an arraylist object with the name "names" and assigns the ArrayList from application variable "Names". Next we are checking if we got a valid arraylist object from application variable. It is possible that user come to this page directly and no value is set to the application variable yet. In this case, you will get null value from application variable. If there is no valid object in application variable, we will display an appropriate error message ( "No names are added to application variable.").

If we get a valid arraylist object from Application variable, then we will loop through the arraylist items and add to the label controls Text property.

Once we have created the ShowNames.aspx page, let us go back to our Login.aspx page and provide a link from that page to our new page.

We have already added a HyperLink control to the Login page. Go to the properties window for this control and set the NavigateUrl property to "ShowNames.aspx". If anyone click on the hyperlink, he will be redirected to the ShowNames.aspx page.

Run the application



Compile and run the application. You can notice couple of things:

1. After you enter your name in the login page, the text box will be hidden and a welcome message will be displayed.
2. The name you entered is added to the application variable.
3. Now you can click on the hyper link and it will redirect to the ShowNames.aspx page, which will display the name you just entered.
4. Open another window. This will create another session. You can enter another name in the login page and go to the ShowNames.aspx page. Now this page will display both the names you entered from both browsers. This is because the application variables retain the values across sessions.
5. You can keep opening any number of new browsers(sessions) and all the login names will be added to the arraylist in application variable.
6. If you reset IIS, the application variables will be cleared.
7. If you go back to the login page after you enter the name once, it will not allow you to enter the name again. This is because browser retains the session until you close browser or it times out (or you restart web server).

Download sample application



If you get confused or cant get the above mentioned sample project properly on your own, you may download the sample project from this site.

Extract the zip file contents into your UserTracker folder, which is already converted into a virtual directory. When you extract files, make sure you do not create another folder within your UserTracker folder.

Open the project file from the extracted files using VS.NET. Simply compile and run the project.

If VS.NET prompt you to set a default page, set the Login.aspx as the default page.

We like to know if you are able to fully understand what we explained in this chapter and previous chapters regarding this small sample ASP.NET project. Please post your feedbacks, questions and comments using the Feedback link below. We will do our best to make things more clear and clarify your doubts.

Download sample project



Next Chapter: Using Cookies to remember user settings
Previous Chapter: Add life to Login page
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India