- Back to Home »
- ASP.NET »
- ASP.NET Displaying Online Users Count and Page Views - Application state in Global.asax
Posted by :
Sudhir Chekuri
Thursday, 2 March 2017
ASP Source code:
<p>
Online User:
<asp:Label ID="lblOnline" runat="server" Text="0"></asp:Label>
</p>
<p>
Page Views:
<asp:Label ID="lblPageViews" runat="server" Text="0"></asp:Label>
</p>
ASP C#.NET Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace StarBus
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//increment on page load and display
Application["PageViews"] = Convert.ToInt32(Application["PageViews"]) + 1;
lblPageViews.Text = Application["PageViews"].ToString();
//display online users
lblOnline.Text = Application["OnlineUsers"].ToString();
}
}
}
Global.asax file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace StarBus
{
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Application_Start(object sender, EventArgs e)
{
Application["OnlineUsers"] = 0;
Application["PageViews"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) + 1;
}
protected void Session_End(object sender, EventArgs e)
{
Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) - 1;
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
<p>
Online User:
<asp:Label ID="lblOnline" runat="server" Text="0"></asp:Label>
</p>
<p>
Page Views:
<asp:Label ID="lblPageViews" runat="server" Text="0"></asp:Label>
</p>
ASP C#.NET Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace StarBus
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//increment on page load and display
Application["PageViews"] = Convert.ToInt32(Application["PageViews"]) + 1;
lblPageViews.Text = Application["PageViews"].ToString();
//display online users
lblOnline.Text = Application["OnlineUsers"].ToString();
}
}
}
Global.asax file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace StarBus
{
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Application_Start(object sender, EventArgs e)
{
Application["OnlineUsers"] = 0;
Application["PageViews"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) + 1;
}
protected void Session_End(object sender, EventArgs e)
{
Application["OnlineUsers"] = Convert.ToInt32(Application["OnlineUsers"]) - 1;
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}