Wiring Events





Wiring Events

If a method is set to execute when an event occurs, it's said to be "wired." Event wire-up can be accomplished declaratively in most cases when it comes to control events or programmatically in any case.

Listing 7.5 showed an example of wiring up an event declaratively by assigning a method name to the OnClick attribute. This is the easiest and most straightforward way to wire up events.

You can also do this programmatically, and in fact you must wire up events programmatically at the application level. We'll demonstrate the application-level events when we talk about HttpModules in Chapter 8, "HttpHandlers and HttpModules."

To programmatically wire up events in a page, you must override the Page class's OnInit method. To best demonstrate this, we'll show the code generated by versions of Visual Studio prior to the 2005 version. The Page directive in the .aspx file includes the attribute AutoEventWireup="false" so that no events, not even Page_Load, are fired automatically. Listing 7.6 shows the code generated in the code-behind file, assuming that there's a button on the form called SendMessageButton.

Programmatically wiring up events in a page
private void Page_Load(object sender, System.EventArgs e)
{
  // Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
  //
  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  //
  InitializeComponent();
  base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
  this.Load += new System.EventHandler(this.Page_Load);
  this.SendMessageButton.Click += new System.EventHandler(this.SendMessageButton_Click);
}
#endregion

private void SendMessageButton_Click(object sender, System.EventArgs e)
{
...
}

The first thing to notice is the method called OnInit. Because we're overriding the method from the Page base class, this method will execute in the normal execution of the page. The method calls the generated InitializeComponent() method, where the actual event wiring takes place. The this keyword refers to the instance of the class itself, and because the page is derived from the Page class, there is a Load event. We add a new event handler, Page_Load, to the Load event. Remember that because the Page directive in the .aspx file has AutoEventWireup="false" in it, no event automatically fires. Finally, the next line wires up our SendMessageButton_Click method to the button's Click event.

If this seems like a lot of work, it is. However, it's inspired by the way a Windows application works and therefore is probably familiar to someone who develops Windows apps. The InitializeComponent() method generates all kinds of code to establish, set properties (like control size and position) of, and wire up controls on a Windows form.


We'll show you how to wire up events to the application in the next chapter.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows