AutoEventWireup Handling Page Events

252 Raised when an unhandled exception is thrown. This provides a last-chance opportunity to gracefully deal with unexpected errors perhaps by displaying a friendly Were sorry, but our web application has barfed message. Its syntax is: Protected Overrides Sub OnErrorByVal e As EventArgs Init inherited from System.Web.UI.Control Raised when the Page object has been created, but before the view state has been loaded. Its syntax is: Protected Overrides Sub OnInitByVal e As EventArgs Load inherited from System.Web.UI.Control Raised when the Page object has finished loading. Its syntax is: Protected Overrides Sub OnLoadByVal e As EventArgs PreRender inherited from System.Web.UI.Control Raised immediately prior to the page rendering itself. Its syntax is: Protected Overrides Sub OnPreRenderByVal e As EventArgs Unload inherited from System.Web.UI.Control Raised after the page has been rendered but before the Page objects Dispose method is called. Its syntax is: Protected Overrides Sub OnUnloadByVal e As EventArgs The last four events are raised on each page request, in the order shown. The three events that are raised prior to page rendering Init, Load, and PreRender are raised at different points during the creation of the page: • The Init event is raised after the Page object has been created but before it has loaded values into its constituent controls or processed any postback data. • The Load event is raised after constituent controls have been loaded with their values. However, postback processing is not guaranteed to be finished. • The PreRender event is raised after all loading and postback processing is finished and the page is about to be rendered into HTML.

6.2.1 AutoEventWireup

If the developer of a web page or code-behind class chooses to handle base-class events explicitly, the event-handler methods must be associated with the events that they are to handle. In Visual Basic .NET this is generally done with a Handles clause in the method definition, as described in Chapt er 2 and shown by example here: Protected Sub Page_Load _ ByVal sender As Object, _ ByVal e As EventArgs _ Handles MyBase.Load ... End Sub In addition to this syntax, ASP.NET provides an alternative way to hook up page event handlers to their associated events. If all of the following conditions are met, the ASP.NET framework automatically hooks up the page events with the correct handler methods: 253 • The AutoEventWireup attribute of the Page directive is set to true which it is by default. This means that the AutoEventWireup attributes value is true if the attribute is omitted from the Page directive, as well as if the Page directive itself is omitted. • The handler-method name is of the form Page_EventName . • The handler-method signature matches the signature of the corresponding event. For example, if these conditions are met, the following code is automatically hooked up to the pages Load event: Protected Sub Page_Load _ ByVal sender As Object, _ ByVal e As EventArgs _ ... End Sub The handler method can appear in either the page or the code-behind class if any. To disallow autowiring of events, specify false for the AutoEventWireup attribute, like this other attributes omitted for brevity: Page AutoEventWireup=false There is no clear advantage to either setting. Event autowiring may feel more familiar to developers who have used Visual Basic 6, where appropriately named methods automatically handled the corresponding events. On the other hand, some developers may feel more comfortable forcing event- handler methods to explicitly declare the events they are handling. In addition, the explicit declaration frees the developer from having to name the method according to the Page_EventName pattern.

6.3 More About Server Controls