Search This Blog

Thursday 4 September 2014

Count Number of Visitors to Website in Asp.net C#

Here I will explain how to count number of visitors to a website in asp.net using C# 

1.open Global.asax file and write the code as shown below

void Application_Start(object sender, EventArgs e)
{
Application["VisitorCount"] = 0;
}
 
void Session_Start(object sender, EventArgs e)
{
Application["VisitorCount"] = (int)Application["VisitorCount"] + 1;
}
 
void Session_End(object sender, EventArgs e)
{
Application["VisitorCount"] = (int)Application["VisitorCount"] - 1;
}

2.Now open your aspx page in which you want to show the count and write the following code. Just add a label where ever you want to display the count

<div>
<asp:Label ID="lblCount" runat="server" ForeColor="Red" /> 
</div>
Now the the below code in the cs file on page load event as shown below.
protected void Page_Load(object sender, EventArgs e)
{
lblCount.Text = Application["VisitorCount"].ToString();
}

No comments:

Post a Comment