Prev | Current Page 285 | Next

Rob Cameron and Dale Michalk

"Pro ASP.NET 3.5 Server Controls and AJAX Components"

ToLongTimeString() + ": No change.";
}
protected void NameTextBox_TextChanged(object sender, EventArgs e)
{
ChangeLabel.Text = DateTime.Now.ToLongTimeString() + ": Changed!";
}
}
}
196 CHAPTER 5 ?–  SE RVER CONTROL EVENTS
In ASP.NET 1.1, the event wiring is conducted, usually by the Visual Studio .NET 2003
designer, inside the InitializeComponent routine:
private void InitializeComponent()
{
this.NameTextBox.TextChanged += new System.EventHandler(this.Name_TextChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
In ASP.NET 2.0 and later, the syntax is much more streamlined; you simply declare the
event handler as an attribute on the server control tag:
OnTextChanged="NameTextBox_TextChanged">
Notice the attribute OnTextChanged is assigned the method name in the code-behind file,
simplifying the page model greatly. Behind the scenes in ASP.NET 2.0 and later, just like in
ASP.NET 1.1, the Name_TextChanged method is wrapped by a System.EventHandler delegate and
then passed to the TextChanged event of our custom TextBox control to add it to its delegate
invocation list. The execution of the web form during the initial page request results in the UI
output of Figure 5-5.


Pages:
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297