Wednesday, May 2, 2012

Working with Global.asax in ASP.net

  It is a Active Server Application file which contains the Application and Session events to keeping the information globally. It is a part of State Management in asp.net that provide to storing data at server side.A website can have only one Global.asax file.

   Step-1

    1. Add new Global.asax file if not added before
    2. Follow the Code below

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

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["NoOfUser"] = 0;
        Application["OnLineUser"] = 0;

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


 Step-2
  
     1. Add new web form file if not added before
     2. Follow the Code below (I have taken it under a master page )
 
   Design page (Source code)

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="FrmGlobal.aspx.cs" Inherits="StateManagement_FrmGlobal" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <div align="center">
   <fieldset style="width:300px;height:200px">
   
       <table class="style1">
         
         
           <tr>
               <td>
                   &nbsp;</td>
               <td>
                  <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                  <br />
                  <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
               </td>
           </tr>
         
       </table>
   
 </fieldset>
</div>
</asp:Content>


C# code



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class StateManagement_FrmGlobal : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Timeout = 1;
        Label2.Text ="Total Number of users are :"+ Application["NoOfUser"].ToString();
        Label1.Text ="On Line users are :" + Application["OnLineUser"].ToString();
    }
}

Every call of the application we can see the result

 

     

No comments:

Post a Comment