Locate the code that starts with public class Global : System.Web.HttpApplication and add the following shaded lines of code:
/// Summary description for Global.
public class Global : System.Web.HttpApplication
private static int totalNumberOfUsers = 0;
private static int currentNumberOfUsers = 0;
/// Required designer variable.
Whenever the Web application starts, the Global class is constructed and the two hit counters will be initialized to 0.
The next step is to add code to the Session_Start event. This event will fire once for each user when they request the first page in your Web application, so this place is perfect for your hit counter. Inside this event, the values of the two hit counters are increased; one for the total number of users and one for the current number of users.
Locate the skeleton for the Session_Start event and add the following code:
protected void Session_Start(Object sender, EventArgs e)
totalNumberOfUsers += 1;
currentNumberOfUsers += 1;
Just as with the Session_Start event, you'll need to write some code for the Session_End event. However, instead of increasing the counters, you should decrease the counter for the current number of users only. This means that whenever a user Session times out (usually 20 minutes after they requested their last page), the counter will be decreased, so it accurately holds the number of current users on your site. You should leave the counter for the total number of users untouched.
Locate the Session_End event, and add this code:
protected void Session_End(Object sender, EventArgs e)
currentNumberOfUsers -= 1;
Making Your Counters Accessible by Other Pages
Since the hit counters are stored in the Global class, you need some way to get them out of there, so you can display them on a management page for example. The easiest way to do this, is to create two public properties. Because the Global class is in many respects just an ordinary class, it is easy to add public properties to it.
To add the properties, locate the skeleton for the Application_End event, and add the following code right below it:
protected void Application_End(Object sender, EventArgs e)
public static int TotalNumberOfUsers
public static int CurrentNumberOfUsers
With these two read-only properties in place, your calling code is now able to access the values of your hit counters. For example, to retrieve the number of users browsing your site right now, you can use this code: HitCounters.Global.CurrentNumberOfUsers where HitCounters is the default namespace for your Web application as defined on the Property Pages for the Web project in Visual Studio .NET.
Testing it Out
To test out your hit counters, create a new Web form and call it HitCounter.aspx. You can save the form anywhere in your site. In Design View, add two labels to the page and call them lblTotalNumberOfUsers and lblCurrentNumberOfUsers respectively. Add some descriptive text before the labels, so it's easy to see what value each label will display. You should end up with something similar to this in Code View:
<form id="frmHitCounter" method="post" runat="server">
Total number of users since the Web server started:
<asp:label id="lblTotalNumberOfUsers" runat="server"></asp:label><br />
Current number of users browsing the site:
<asp:label id="lblCurrentNumberOfUsers" runat="server"></asp:label><br />
Press F7 to view the Code Behind for the hit counter page, and add the following code to the Page_Load event:
private void Page_Load(object sender, System.EventArgs e)
int currentNumberOfUsers = HitCounters.Global.CurrentNumberOfUsers;
int totalNumberOfUsers = HitCounters.Global.TotalNumberOfUsers;
lblCurrentNumberOfUsers.Text = currentNumberOfUsers.ToString();
lblTotalNumberOfUsers.Text = totalNumberOfUsers.ToString();
}Suneel161, if this helps please login to Mark As Answer. | Alert Moderator