Be Careful when Changing Global.asax
Be cautious when you add and modify code within the Global.asax file.
Any additions or modifications you make within this file will cause the ap-
plication to restart, so youll lose any data stored in application state.
Using Application State
You can store the variables and objects you want to use throughout an entire
application in a special object called Application. The data stored in this object
is called application state. The Application object also provides you with
methods that allow you to share application state data between all the pages in
a given ASP.NET application very easily.
Application state is closely related to another concept: session state. The key
difference between the two is that session state stores variables and objects for
one particular user for the duration of that users current visit, whereas application
state stores objects and variables that are shared between all users of an application
at the same time. Thus, application state is ideal for storing data thats used by
all users of the same application.
In ASP.NET, session and application state are both implemented as collections,
or sets of name-value pairs. You can set the value of an application variable named
SiteName like this:
Visual Basic
Application("SiteName") = "Dorknozzle Intranet Application"
C#
Application["SiteName"] = "Dorknozzle Intranet Application";
With SiteName set, any pages in the application can read this string:
Visual Basic
Dim appName As String = Application("SiteName")
C#
String appName = Application["SiteName"];
We can remove an object from application state using the Remove method, like
so:
Visual Basic
Application.Remove("SiteName")
173
Using Application State
C#
Application.Remove("SiteName");
If you find you have multiple objects and application variables lingering in applic-
ation state, you can remove them all at once using the RemoveAll method:
Visual Basic
Application.RemoveAll()
C#
Application.RemoveAll();
Its important to be cautious when using application variables. Objects remain
in application state until you remove them using the Remove or RemoveAll
methods, or shut down the application in IIS. If you continue to save objects
into the application state without removing them, you can place a heavy demand
on server resources and dramatically decrease the performance of your applica-
tions.
Lets take a look at application state in action. Application state is very commonly
used to maintain hit counters, so our first task in this example will be to build
one! Lets modify the Default.aspx page that Visual Web Developer created
for us. Double-click Default.aspx in Solution Explorer, and add a Label control
inside the form element. You could drag the control from the Toolbox (in either
Design View or Source View) and modify the generated code, or you could simply
enter the new code by hand. Well also add a bit of text to the page, and change
the Labels ID to myLabel, as shown below:
File: Default.aspx (excerpt)
<form id="form1" runat="server">
<div>
The page has been requested
<asp:Label ID="myLabel" runat="server" />
times!
</div>
</form>
In Design View, you should see your label appear inside the text, as shown in
Figure 5.27.
Now, lets modify the code-behind file to use an application variable that will
keep track of the number of hits our page receives. Double-click in any empty
space on your form; Visual Web Developer will create a Page_Load subroutine
automatically, and display it in the code editor.
174
Chapter 5: Building Web Applications
Figure 5.27. The new label appearing in Design View
Visual Basic File: Default.aspx.vb (excerpt)
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
C# File: Default.aspx.cs (excerpt)
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Now, lets modify the automatically generated method by adding the code that
we want to run every time the page is loaded. Modify Page_Load as shown below:
Visual Basic File: Default.aspx.vb (excerpt)
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' Reset counter when it reaches 10
If Application("PageCounter") >= 10 Then
Application.Remove("PageCounter")
End If
' Initialize or increment page counter each time the page loads
If Application("PageCounter") Is Nothing Then
Application("PageCounter") = 1
Else
Application("PageCounter") += 1
175
Using Application State

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.