Monday, 18 February 2013

Application and the Session level events using Global.asax


Application and the Session level events using Global.asax

This file exposes the application and session level events in ASP.NET and provides a gateway to all the application and the session level events in ASP.NET. This file can be used to implement the important application and session level events such as Application_Start, Application_End, Session_Start, Session_End, etc.



Events in the Global.asax file :

Application_Init

The Application_Init event is fired when an application initializes the first time.

Application_Start

The Application_Start event is fired the first time when an application starts.

Session_Start

The Session_Start event is fired the first time when a user’s session is started. This typically contains for session initialization logic code.

Application_BeginRequest

The Application_BeginRequest event is fired each time a new request comes in.

Application_EndRequest

The Application_EndRequest event is fired when the application terminates.

Application_AuthenticateRequest

The Application_AuthenticateRequest event indicates that a request is ready to be authenticated. If you are using Forms Authentication, this event can be used to check for the user’s roles and rights.

Application_Error

The Application_Error event is fired when an unhandled error occurs within the application.

Session_End

The Session_End Event is fired whenever a single user Session ends or times out.

Application_End

The Application_End event is last event of its kind that is fired when the application ends or times out. It typically contains application cleanup logic.

Example :

//Fired the first time when an application starts.

 void Application_Start(Object Sender, EventArgs e)
 {
   Application["Hits"] = 0;
   Application["Sessions"] = 0;
   Application["TerminatedSessions"]=0;

   or

   RETRIVE VALUES FROM DATABASE  
 }

 //The BeginRequest event is fired for every hit to every page in the site

 void Application_BeginRequest(Object Sender, EventArgs e)
{
 Application.Lock();
 Application["Hits"] = (int) Application["Hits"] + 1;
 Application.UnLock();
 }

 //Fired when a new user visits the application Web site.

 void Session_Start(Object Sender, EventArgs e)
 {
 Application.Lock();
 Application["Sessions"] = (int) Application["Sessions"] + 1;
 Application.UnLock();
 }

// Fired when a user’s session times out, ends, or they leave the application Web site

 void Session_End(Object Sender, EventArgs e)
 {
 Application.Lock();
 Application["TerminatedSessions"] =
 (int) Application["TerminatedSessions"] + 1;
 Application.UnLock();
 }

//Fired when the last instance of an HttpApplication class is destroyed. It’s fired only once during an application’s lifetime

 void Application_End(Object Sender, EventArgs e)
 {
 SAVE VALUES (HITS, SESSIONS) IN DATABASE
 }

//User count
label1.text=application["Sessions"].tostring;

//Hits
label2.text=application["Hits"].tostring;

(or)

Retrive values from database and put count in lable

this will give the no of visitors in ur page

No comments:

Post a Comment