[ Team LiB ] |
12.5 Events Reference
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
ExampleThe 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 NotesThe 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.
Parameters
ExampleThe 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> NotesThe 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.
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:
ExampleSee the example for Init. NotesNote 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.
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:
ExampleThe 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 NotesWhile 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 ] |