Article

Show number of visitors in asp.net application

08 Mar 2018 vikram singh
0 Comments 993 Views



Sometimes we need to display how to show online visitors in asp.net website. Here I will be explaining how we can count number of visitors of a particular website in asp.net.

 

To achieve this, simply we need to add Global.asax in our application.

Steps for this would be as follow-

1- Open visual studio

2- Create New website

3- Right click on solution explorer bar

4- then select add new item

5- select global application class file from given options

6- click ok and you are done.

Global.asax

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["NoOfVisitor"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();
}

ASPX Page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Freshcode Hub</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <b>No of Visits:</b>
                </td>
                <td>
                    <asp:Label ID="lblCountVisits" runat="server" ForeColor="Red" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

CS Page

protected void Page_Load(object sender, EventArgs e)
    {
        lblCountVisits.Text = Application["NoOfVisitors"].ToString();
    }

 

vikram singh

I am asp.net developer with 3+ years exp. in asp.net, jquery, javascript,SQL area knowledege.

Comments

No coments found to display!

Leave a Comment