Create and Use Session and Application Level Events in ASP.NET

Introduction

Global.asax file is also known as ASP.NET application file. It provides a way to respond to application or module level events in one central location. We can use this file to implement application security, total hits counting, number of users online as well as other tasks. The Global.asax file exists in the root of the application directory. Global.assx file is an optional file so if we don't need then simply delete it from application and no any other change required. The Global.asax file is configured so that any direct HTTP request (via URL) is rejected automatically, so users cannot download or view its contents. The ASP.NET page framework recognizes automatically any changes that are made to the Global.asax file. The framework reboots the application, which includes closing all browser sessions, flushes all state information, and restarts the application domain. 

About Global.asax page

Adding a Global.asax to your web project is quiet simple. Open Visual Studio 2005 or 2008 > Create a new website > Go to the Solution Explorer > Add New Item > Global Application Class > Add. 

Now we will have three Application Event Handlers and two Session Event Handlers. 

Here is the list of Application Event Handlers:

Sub Application_Start(ByVal sender As ObjectByVal e As EventArgs)
' Code that runs on application startup
End Sub
    
Sub Application_End(ByVal sender As ObjectByVal e As EventArgs)
' Code that runs on application shutdown
End Sub
        
Sub Application_Error(ByVal sender As ObjectByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub

Here is the list of Session Event Handlers:

Sub Session_Start(ByVal sender As ObjectByVal e As EventArgs)
' Code that runs when a new session is started
End Sub

Sub Session_End(ByVal sender As ObjectByVal e As EventArgs)
' 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.
End Sub

Event Handlers will be used

Application_Start():
It gets fired when the first resource is requested from the web server and the web application starts.
Application_BeginRequest():
It gets fired when an application request is received. It's the first event fired for a request, which is often a page request (URL) that a user enters.

Working with Global.asax file

<%@ Application Language="VB" %>
<script runat="server">

    Sub Application_Start(ByVal sender As ObjectByVal e As EventArgs)
        ' Code that runs on application startup
        Application("TitleOfPage") = "USER DETAILS STATEMENT"
        Application("NumberOfOnlineUsers") = 0
        Application("NumberOfUsers") = 0
    End Sub

    Sub Application_BeginRequest(ByVal sender As ObjectByVal e As EventArgs)
        Application.Lock()
        Application("NumberOfUsers") = CType(Application("NumberOfUsers"), Integer) + 1
        Application.UnLock()
    End Sub

    Sub Application_End(ByVal sender As ObjectByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub

    Sub Application_Error(ByVal sender As ObjectByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
    End Sub

    Sub Session_Start(ByVal sender As ObjectByVal e As EventArgs)
        ' Code that runs when a new session is started
        Application.Lock()
        Application("NumberOfOnlineUsers") = CType(Application("NumberOfOnlineUsers"), Integer) + 1
        Application.UnLock()
    End Sub

    Sub Session_End(ByVal sender As ObjectByVal e As EventArgs)
        ' 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.
    End Sub
       
</script>

Working with Default.aspx file

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default"%>

<!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>Create and Use Session and Application Level Events in ASP.NET</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td colspan="3">
                    <asp:Label ID="Label1" runat="server" Text="Label" Width="561px" Font-Bold="True"Font-Size="Large" ForeColor="Red"></asp:Label><br />
                    <br />
                </td>
            </tr>
            <tr>
                <td style="width: 68px">
                    <asp:Label ID="Label4" runat="server" Font-Bold="True" ForeColor="Blue"Text="Number of Online Users"
                        Width="252px"></asp:Label></td>
                <td colspan="2">
                    <asp:Label ID="Label2" runat="server" Text="Label" Width="410px"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 68px">
                    <asp:Label ID="Label5" runat="server" Font-Bold="True" ForeColor="Blue"Text="Number of Users Served (Hits)"
                        Width="395px"></asp:Label></td>
                <td colspan="2">
                    <asp:Label ID="Label3" runat="server" Text="Label" Width="411px"></asp:Label></td>
            </tr>
        </table>

      </div>
    </form>
</body>
</
html>

Working with Default.aspx.vb file

Partial Class _Default
    Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = Application("TitleOfPage")
        Label2.Text = Application("NumberOfOnlineUsers")
        Label3.Text = Application("NumberOfUsers")
End Sub
End Class

HAVE A HAPPY CODING!


Similar Articles