Chapter 14A. Event Model in Web Forms

An event is something that occurs in response to an action. The ASP.NET Web Form framework provides a rich event model for the developer. In previous lessons you have seen that by simply double-clicking a button the IDE adds an event handler for the button's click event. In this lesson I show you how to handle other types of events, use one event handler for multiple objects, and force a postback to occur when a certain event is triggered.

Lesson 8A covered the page's life cycle. This is the sequence of events that are raised whenever a page is requested:

  • PreInit

  • Init

  • InitComplete

  • PreLoad

  • Load

  • LoadComplete

  • PreRender

  • PreRenderComplete

  • SaveStateComplete

  • Unload

By default, page events are automatically bound to methods with the name Page_event. This is true because by default AutoEventWireup="true" is included in the @Page directive for every page. If AutoEventWireup is set to false, you have to wire-up the events yourself. This is the code to add an event handler called LoadMyPage to handle the Load event of the page:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.Load += new EventHandler(LoadMyPage);
}

Like pages, server controls also have a life cycle. Server control events are processed after the pages Page_Init and Page_Load events. This is the sequence of events that is followed for every server control whenever a page is requested:

  • Init

  • Load

  • PreRender

  • DataBinding

  • Unload

  • Disposed

However, most server controls include additional events in their ...

Get ASP.NET 4 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.