In previous post I explained clearly about Global.asax file uses and events in asp.net. Here I will explain how to add code behind file to Golbal.asax in asp.net. In one of my project I started writing code in Global.asax file but we decided to write code in Global.asax.cs file code I tried to search for that one but that is not available in our application at that I learn one thing that is we need to add Global.asax and Global.asax.cs these files to our application. For that one first right click on solution explorer and select Add New item


After that select Global Application class file and click ok now Global.asax file added successfully to our application


After add our Global.asax file to application that would be like this


And our Global.asax file contains code like this

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

<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 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)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
}
</script>
Now our requirement is to add Global.asax.cs file to our code for that again right click on solution explorer and select Add new item another wizard will open in that select Class file and give name as Global.asax.cs and click Add


After click Add that will show one warning message like this now click yes


After adding our Global.asax.cs file we need make some changes to use Global.asax.cs file those are first remove entire script code from your Global.asax file

<script runat="server">
---------------------------
---------------------------
---------------------------
</script>
And change this line in Global.asax file to

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

<%@ Application Language="C#" CodeBehind="~/App_Code/global.asax.cs" Inherits="Global" %>
Now open Global.asax.cs file and change this like

public class Global
To

public class Global:System.Web.HttpApplication

Now write the events in Global.asax.cs file and check with break point it will work for you

Comments (1)

On 27 August 2012 at 04:29 , Unknown said...

its really help me lot my task.
php training in ahmedabad