Application state is stored in an instance of the HttpApplicationState class. This class exposes a key-value dictionary of objects.
The HttpApplicationState instance is created the first time a user accesses any URL resource in an application. The HttpApplicationState class is most often accessed through the Application property of the HttpContext class.
You can use application state in two ways. You can add, access, or remove values from the Contents collection directly through code. The HttpApplicationState class can be accessed at any time during the life of an application. However, it is often useful to load application state data when the application starts. To do so, you can put code to load application state into the Application_Start method in the Global.asax file. For more information see ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0.
Alternatively, you can add objects to the StaticObjects collection via an <object runat="server"> declaration in your Web application's Global.asax file. Application state defined in this way can then be accessed from code anywhere in your application. The following example shows an object declaration for an application state value:
<object runat="server" scope="application" ID="MyInfo"
PROGID="MSWC.MYINFO">
</object>
You can add objects to the StaticObjects collection only in the Global.asax file. The collection throws a NotSupportedException if you attempt to add objects directly through code.
You can access members of objects stored in application state without having to reference the Application collection. The following code example shows how to reference a member of an object defined in the StaticObjects collection of application state:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = MyInfo.Title
End Sub
protected void Page_Load(Object sender, EventArgs e)
Label1.Text = MyInfo.Title;
End Sub