Prev | Current Page 280 | Next

Rob Cameron and Dale Michalk

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


Adding an Event to the TextBox Control
The TextBox control that we started in Chapter 3 had the beginnings of a nice clone of the
ASP.NET System.Web.UI.WebControls.TextBox control. It saves its values to ViewState, emits
the correct HTML to create a text box in the browser, and handles postback data correctly. The
control is well on its way to becoming a respectable member of the family.
We next enhance our TextBox control by adding the capability to raise an event when the
Text property of the control has changed, as detected by comparing the value currently stored
in ViewState with postback data.
Enhancing the TextBox Control with a TextChanged Event
The next step in our TextBox journey is to add a TextChanged event to help bring its functionality
more in line with that of the built-in ASP.NET text controls. This necessitates adding an event
declaration and enhancing the implementation of the IPostBackDataHandler interface in our
control. The most important upgrade is the addition of the TextChanged event field and a protected
OnTextChanged method to invoke it:
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
TextChanged(this, e);
}
public event EventHandler TextChanged;
192 CHAPTER 5 ?–  SE RVER CONTROL EVENTS
The second upgrade is the logic enhancement to the LoadPostData and RaisePostDataChanged
methods.


Pages:
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292