Compartir a través de


Code Declaration Blocks — Global.asax

Code declaration blocks provide a way to define application member variables, event handlers, and methods. These code elements are then available for use by the compiled HttpApplication class.

The syntax for a code declaration block is as follows.

<script runat="server" language="language" src="externalfile">
   Code goes here.
</script>

You can use as many code declaration blocks as you want.

Attributes

  • runat
    Set runat="server" to run the code block in your application.
  • language
    The language used in the current code declaration block. The language can be C#, Visual Basic, or JScript.
  • src
    The name of a script file containing code that is loaded and used in the current code declaration block. When this attribute is used, any other code in the declaration block is ignored.

Remarks

Code declaration blocks are defined using <script> tags that contain a runat attribute value set to server. The <script> tag can optionally use a language attribute to specify the language of its inner code. If none is specified, ASP.NET defaults to the language configured for the application (specified in the application's XML configuration file).

Example

The following code example demonstrates how a <script runat=server> block can be used within an ASP.NET application file to define four event handlers.

<script language="VB" runat="server">

   ' You can also use Application_Start for the method name in the
   ' following declaration.  
   Sub Application_OnStart()
      ' Application startup code goes here.
   End Sub

   ' You can also use Application_Start for the method name in the
   ' following declaration.  
   Sub Session_OnStart()
      ' Session startup code goes here.
   End Sub

   ' You can also use Application_Start for the method name in the
   ' following declaration.  
   Sub Session_OnEnd()
      ' Session cleanup code goes here.
   End Sub

   ' You can also use Application_Start for the method name in the
   ' following declaration.  
   Sub Application_OnEnd()
      ' Application cleanup code goes here.
   End Sub

   Overrides Sub HandleError(ErrorInfo as Exception)
      ' Application error handling code goes here.
   End Sub

</script>

See Also

Global.asax Syntax | Directives