# Count no. of active users on website :
Add this code on your Global.asax Page
public void Session_Start(object sender, EventArgs e)
{
//Fires when the session is started.
Application["UserCount"] = Convert.ToInt32(Application["UserCount"].ToString()) + 1;
}
public void Session_End(object sender, EventArgs e)
{
//Fires when the session ends.
Application["UserCount"] = Convert.ToInt32(Application["UserCount"].ToString()) - 1;
}
Add this code on your Master Page
private void Page_Load(System.Object sender, System.EventArgs e)
{
lblUserCount.Text = "Users online " + Application["UserCount"].ToString();
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["OnlineUsers"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate
// mode is set to InProc in the Web.config file.
// If session mode is set to StateServer or SQLServer,
// the event is not raised.
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
Application.UnLock();
}
Add this code on your Master Page source code
<div>
<asp:Label ID="lblUserCount" runat="server" Text="Label"></asp:Label>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
Add this code on your Master Page code Behind
protected void Page_Load(object sender, EventArgs e)
{
lblUserCount.Text = "Users online " + Application["OnlineUsers"].ToString();
}