Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Monday, 16 January 2012

Count Number of Visitors in WebSite using ASP.Net and C#

Write Code in Global.asax file


<%@ Application Language="C#" %>

<script runat="server">
    public static int count = 0;
    void Application_Start(object sender, EventArgs e) 
    {
        Application["myCount"] = count; 
        // Code that runs on application startup

    }
    
    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) 
    {
        count = Convert.ToInt32(Application["myCount"]); Application["myCount"] = count + 1;
        // Code that runs when a new session is started

    }

    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.

    }
       
</script>

.CS Code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Hitcounter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int a;
         a = Convert.ToInt32((Application["myCount"]));
          Label1.Text = Convert.ToString(a);
        if (a < 10)
            Label1.Text = "000" + Label1.Text;
        else if (a < 100)
            Label1.Text = "00" + Label1.Text;
        else if (a < 1000)
            Label1.Text = "0" + Label1.Text;
    }
}

1 comment: