Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] - نسخه متنی

Jeffrey Putz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






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.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.

Listing 7.6. 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.


/ 146