Application State

   

Application state was covered in Chapter 4, which addressed the Application property of the ASP.NET Page object. I explained that the Application property is really an instance of the HttpApplicationState object and that you can initialize an Application variable in the Application_OnStart event that is contained in the Global.asax file located in the root of your application.

First you must remember that Application variables are just like Session variables, except that they are shared across all users. A Session variable holds information that is unique to that user. Application variables hold information that is common to all users.

To reiterate what was covered in Chapter 4, first look at the global.asax file to see how you can initialize Application variables.

Visual Basic .NET global.asax
<script language="vb" runat=server>  Sub Application_OnStart()      Application("Publisher") = "New Riders"      Application("BookTitle") = "ASP.NET for Web Designers"      Application("Author") = "Peter"      Application("Rating") = "5 Stars, WHAHOOO!!"  End Sub  </script> 
C# global.asax
<script language="c#" runat=server>  void Application_OnStart(){     Application["Publisher"] = "New Riders";      Application["BookTitle"] = "ASP.NET for Web Designers";      Application["Author"] = "Peter";      Application["Rating"] = "5 Stars, WHAHOOO!!";  }  </script> 

This sets the listed Application variables when the application first starts, which happens when the first person makes the first request to your web site.

The following are the examples from Chapter 4 that show how to retrieve an Application variable. It is no different than retrieving a Session variable, except you address the Application instead of the Session.

Visual Basic .NET page_application_vb.aspx
<%@ page language="vb" runat="server"%>  <script  runat=server>  Sub Page_Load()      Title.Text = "<u>Title:</u> " + Application("BookTitle")      Publisher.Text = "<u>Publisher:</u> " + Application("Publisher")      Author.Text = "<u>Author:</u> " + Application("Author")      BookRating.Text = "<u>Rating:</u> " + Application("Rating")  End Sub  </script>  <html>  <title>Application</title>  <body>  <asp:label  runat="server"/><br>  <asp:label  runat="server"/><br>  <asp:label  runat="server"/><br>  <asp:label  runat="server"/>  </body>  </html> 
C# page_application_cs.aspx
<%@ page language="cs" runat="server"%>  <script  runat=server>  void Page_Load(){     Title.Text = "<u>Title:</u> " + Application["BookTitle"];      Publisher.Text = "<u>Publisher:</u> " + Application["Publisher"];      Author.Text = "<u>Author:</u> " + Application["Author"];      BookRating.Text = "<u>Rating:</u> " + Application["Rating"];  }  </script>  <html>  <title>Application</title>  <body>  <asp:label  runat="server"/><br>  <asp:label  runat="server"/><br>  <asp:label  runat="server"/><br>  <asp:label  runat="server"/>  </body>  </html> 

Application variables can be used to hold any type of global information such as database connection. Earlier in this chapter, while describing the Session_Start and Session_End events, an Application variable was actually utilized to keep a running count of active sessions on a web application. There are many uses for Application variables for storing global data.


   
Top


ASP. NET for Web Designers
ASP.NET for Web Designers
ISBN: 073571262X
EAN: 2147483647
Year: 2005
Pages: 94
Authors: Peter Ladka

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net