The global.asax file is located at the root of your application's directory tree. It handles high-level application events such as Application_Start , Application_End , Session_Start , and Session_End . This file is parsed and compiled into a dynamic .NET framework class the first time any resource or URL in your application's namespace is used. The global.asax file is configured so that it rejects direct requests by URLs. This prevents users from gaining access to the code in the global.asax file.

Application or Session-Scoped Events

You can define handlers for HttpApplication events by writing methods in the global.asax file. These must conform to the naming pattern Application _ EventName ( AppropriateEventArgumentSignature ) . For example:

In C#:

 <script language="C#" runat="server">    void Application_Start() {      // Application startup code goes here      }  </script> 

In Visual Basic:

 <script language="VB" runat="server">    Sub Application_Start()     ' Application startup code goes here    End Sub  </script> 

In JScript:

 <script language="JScript" runat="server">    function Application_Start() : void {       // Application startup code goes here    }  </script> 

If you have the need, you may import additional namespaces using the Import directive on an .aspx page:

 <%@ Import Namespace="System.Text" %> 

The global.asax file enables you to control the scope or lifetime of both session and application events. In short, you can control whether an event lasts the duration of the application, the duration of a particular session in the application, or for one instance of the application or session.

The same is true of objects. They can be maintained throughout the duration of the application, throughout the session, or for one instance of the application or session. Object tags are used to define objects in the global.asax file. The scope of these objects can be appinstance , session , or application , as in the following code:

 <object id="id" runat="server" class=".NET Framework class Name" scope="appinstance">  <object id="id" runat="server" progid="Classic COM ProgID" scope="session"/>  <object id="id" runat="server" classid="Classic COM ClassID" scope="application"/> 

The appinstance scope makes the object specific to one instance of the HttpApplication and therefore is not shared.

For a further example of the use of scope with objects and its benefits, consider the following code example. In this example, a file is read in at Application_Start() and stored in a DataView object. It then is essentially a read-only file. Obviously, data with an applicationwide scope should be data that is modified only very infrequently. The benefit of this arrangement is that only the first request incurs the penalty for retrieving the data; all later requests use the already existing object.

 Sub Application_Start()    Dim ds As New DataSet()    Dim fs As New FileStream(Server.MapPath("schemadata.xml"), FileMode.Open,FileAccess. graphics/ccc.gif Read)    Dim reader As New StreamReader(fs)    ds.ReadXml(reader)    fs.Close()    Dim view As New DataView (ds.Tables(0))    Application("Source") = view  End Sub 

In the PageLoad event, the data is retrieved and put into a DataGrid object.

 Sub Page_Load(Src As Object, E As EventArgs)    Dim Source As New DataView = CType(Application("Source"), DataView)    ...    MyDataGrid.DataSource = Source    ...  End Sub 

Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233 © 2008-2017.
If you may any questions please contact us: