Wednesday, February 3, 2010

Creating Hit Counter in ASP.NET WebSite

Create a Website (website1)
Add a Class file and edit like:
==================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

///
/// Summary description for Global
///

public class Global:HttpApplication
{
public static int Users = 0;
public Global()
{
//
// TODO: Add constructor logic here
//
}
}
====================
Add a Global Application Class (Global.asax) file and edit like:
in session_start Event Increment the number of users, means on every session the number of users will be incremented.
====================
void Session_Start(object sender, EventArgs e)
{
Global.Users++;
}
=============================
Then goto your Default.aspx.cs page source and edit like:
=======================
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("No Of Users:"+Global.Users);
}
}

================
Execute this Application First time you will see No of Users: 1
break this application execute next time you will see No of users: 2
and the same will be keep going for next Consecutive page requests.
========
Enjoy...