This article shows how to use this file in your application development.
Global.asax file
The Global.asax file is in the root application directory. While Visual Studio .NET automatically inserts it in all new ASP. NET projects. Special class file that holds some methods that execute automatically when any page of the website is invoked. This file contains following methods.
Session_Start() - Fired when a new user visits the application Web site.
Session_End() - Fired when a user's session times out, ends, or they leave the application Web site.
Application_Start() - Fired when the first instance of the HttpApplication class is created. It allows you to create objects that are accessible by all HttpApplication instances.
Application_End() - Fired when the last instance of an HttpApplication class is destroyed. It's fired only once during an application's lifetime.
Adding this class from in the project.
Project -> Add New Item… -> Global Application Class.
For example: Suppose we want to show a message all the web page in the project such as Happy Dipawali.
Drag a label and a button control on the form. Form looks like this.
Figure 1.
Now double click on the form and add the following code.
VB code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Application("message") Is Not Nothing Then
Label1.Text = Application("message").ToString()
End If
End Sub
Now double click on the Button control and add the following code.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Server.Transfer("webForm2.aspx")
End Sub
C# code:
protected void Page_Load(object sender, EventArgs e)
{
if (Application["message"] != null)
lblMessage.Text = Application["message"].ToString();
}
Now double click on the Button control and add the following code.
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("webForm2.aspx");
}
Now open the global.aspx file from the solution explorer.
Figure 2.
And write the following code to application_start method.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
Application("Message") = " Happy Dipawali"
End Sub
Now adding a new form on the project and drag a label control on the form and double click on the form and add the following code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loa
Label1.Text = Application("message").ToString()
End Sub
save and run the application.
Figure 3.
Now click on the button it will show same message on the form two.
Figure 4.