To display online users in ASP.NET application u need to add a
Global.asax file. Global.ascx is the ASP.NET application file. It has some application level events raised by ASP.NET application. In the below code whenever a user log in to your application a count will increase in Application["CurrentUsers"] state. The count will decrease when he Log Out.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["CurrentUsers"] = 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["CurrentUsers"] = (int)Application["CurrentUsers"] + 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();
if ((int)Application["CurrentUsers"] > 0)
{
Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;
}
Application.UnLock();
}
Then in the Code behind add the below code
Label1.Text = Convert.ToString(Application["CurrentUsers"]);
No comments:
Post a Comment