DekGenius.com
[ Team LiB ] Previous Section Next Section

12.5 Events Reference

Error

Sub Page_Error(Sender As Object, e As Event Args)
  'error handling code
End Sub

The Error event is fired when an unhandled exception occurs on the page. If no event handler is defined for this event, the Application_Error event is fired. If the exception is still not handled, control is passed to the page (or pages) defined in the <customErrors> element in web.config.

Parameters

Sender

An argument containing information about the object that raised the event.

e

An object of type EventArgs containing additional information about the event.

Example

The following code example deliberately causes an overflow exception and then handles that exception in the Page_Error handler, displaying the text of the exception and then clearing it:

Sub Page_Load( )
   Dim x, y, overflow As Integer
   x = 1
   y = 0
   overflow = x / y
End Sub
  
Sub Page_Error( )
   Response.Write(Server.GetLastError.ToString( ))
   Server.ClearError
End Sub

Notes

The current exception is obtained using the GetLastError method of the Server class. Once you've finished with your error handling, you can either clear the exception by calling Server.ClearError, as shown in the example, or allow the exception to bubble up to the next level of error handling.

Note that the Sender and e arguments are optional for this event, as shown in the example.

When the AutoEventWireup attribute of the @ Page directive is set to True (the default), ASP.NET will automatically call the event handler for this event, as long as it has the correct Page_Error signature.

Init

Sub Page_Init(Sender As Object, e As EventArgs)
  'initialization code
End Sub

Parameters

Sender

An argument containing information about the object that raised the event.

e

An object of type EventArgs containing additional information about the event.

Example

The code example initializes a variable for setting the ForeColor property of a label in Page_Init, and then modifies that value to set the ForeColor property of another label in Page_Load:

<%@ Page Language="vb" %>
<html>
   <head>
      <title>Init event example</title>
      <script runat="server">
         Dim TheColor As System.Drawing.Color
         Sub Page_Init( )
            TheColor = System.Drawing.Color.Red
         End Sub
         Sub Page_Load( )
            Message.ForeColor = TheColor
            Message.Text = "The color of the text was set in Page_Init."
            TheColor = System.Drawing.Color.Blue
            Message2.ForeColor = TheColor
            Message2.Text = "The color of the text was set in Page_Load."
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
   <br/>
   <asp:label id="Message2" runat="server"/>
</body>
</html>

Notes

The Sender and e arguments are optional for this event, as shown in the example.

When the AutoEventWireup attribute of the @ Page directive is set to True (the default), ASP.NET will automatically call the event handler for this event, as long as it has the signature Page_Init.

Load

Sub Page_Load(Sender As Object, e As EventArgs)
  'code
End Sub

Fired when the page is loaded. Since this event is fired on every page request, we can add any initialization code that needs to be executed at the page level, including the initialization of the page's child controls. When the Load event fires, the page's view state information is also accessible.

The Load event is passed the following arguments by ASP.NET:

Sender

An argument containing information about the object that raised the event.

e

An object of type EventArgs containing additional information about the event.

Example

See the example for Init.

Notes

Note that the Sender and e arguments are optional for this event, as shown in the example.

When the AutoEventWireup attribute of the @ Page directive is set to True (the default), ASP.NET will automatically call the event handler for this event, as long as it has the correct Page_Load event signature.

Unload

Sub Page_Unload(Sender As Object, e As EventArgs)
  'cleanup code
End Sub

Fired when the page is unloaded from memory. Since this event is fired before the page is unloaded, we can perform cleanup operations, such as closing open files and database connections.

The Unload event is passed the following arguments by ASP.NET:

Sender

An argument containing information about the object that raised the event.

e

An object of type EventArgs containing additional information about the event.

Example

The example demonstrates the Unload event by closing a file that was opened for display in the Page_Load event handler:

Dim TheFile As System.IO.StreamReader
Sub Page_Load( )
   TheFile = System.IO.File.OpenText(MapPath("Init.aspx"))
   Message.Text = "<pre>" & _
      Server.HtmlEncode(TheFile.ReadToEnd( )) & "</pre>"
End Sub
  
Sub Page_Unload( )
   TheFile.Close( )
End Sub

Notes

While the Unload event is useful for performing page-level cleanup tasks, for resources such as databases, for which it is possible that an exception will interrupt the normal flow of page processing, it may be better to place the cleanup code for that resource in the Finally block of a Try...Catch...Finally statement, which will ensure that the cleanup code is always executed. For more information on Try...Catch...Finally, see Chapter 10.

Note that the Sender and e arguments are optional for this event, as shown in the example.

When the AutoEventWireup attribute of the @ Page directive is set to True (the default), ASP.NET will automatically call the event handler for this event, as long as it has the signature Page_Unload.

    [ Team LiB ] Previous Section Next Section