Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Monday, 23 March 2015

Introduction: In this article i will explain with example How to count the number of times the website is visited by the visitors/users and the users currently online on the website.

Steps to Follow:
1.        First of all we need to add global.asax file.
  1. Open the global.aspx file and write the code on the Application_Start, Session_Start and the Session_End event as:

void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["SiteVisitedCounter"] = 0;
        //to check how many users have currently used our site
        Application["OnlineUserCounter"] = 0;
    }

   void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Application.Lock();
        Application["SiteVisitedCounter"] = Convert.ToInt32(Application["SiteVisitedCounter"]) + 1;
        //to check how many users have currently opened our site write the following line
        Application["OnlineUserCounter"] = Convert.ToInt32(Application["OnlineUserCounter"]) + 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["OnlineUserCounter"] = Convert.ToInt32(Application["OnlineUserCounter"]) - 1;
        Application.UnLock();
    }

Design Section:

<div>
    <fieldset style ="width:220px;">
    <legend>Count site visited and Online users</legend>   
        <asp:Label ID="lblSiteVisited" runat="server" Text=""
            style="color: #FFFFFF; background-color: #3366FF"></asp:Label><br />
        <asp:Label ID="lblOnlineUsers" runat="server" Text=""
            style="color: #FFFFFF; background-color: #009933"></asp:Label><br />
       
        <asp:Button ID="btnClearSesson" runat="server" Text="Clear Session"
            onclick="btnClearSesson_Click" />
        </fieldset>
        </div>

C#.Net code
  • In the code behind file(default.aspx.cs) write the code as:

protected void Page_Load(object sender, EventArgs e)
    {
        lblSiteVisited.Text = "No of times site visited=" + Application["SiteVisitedCounter"].ToString();       
        lblOnlineUsers.Text = "No of users online on the site=" + Application["OnlineUserCounter"].ToString();
    }

    protected void btnClearSesson_Click(object sender, EventArgs e)
    {
        Session.Abandon();
    }

VB.Net Code
Design the page same as described in the Source code section above but replace the line        <asp:Button ID="btnClearSesson" runat="server" Text="Clear Session"    onclick="btnClearSesson_Click" />
with
         <asp:Button ID="btnClearSesson" runat="server" Text="Clear Session" /> 
Then In the code behind file(default.aspx.vb) write the code as:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblSiteVisited.Text = "No of times site visited=" & Application("SiteVisitedCounter").ToString()
        lblOnlineUsers.Text = "No of users online on the site=" & Application("OnlineUserCounter").ToString()
    End Sub

Protected Sub btnClearSesson_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesbtnClearSesson.Click
        Session.Abandon()

    End Sub
Categories: , ,

0 comments:

Post a Comment