FREE BOOK

Chapter 5: Event-Based Programming

Posted by Apress Free Book | ASP.NET January 02, 2009
In this chapter, we explore the intricacies of working with server control events.

Summary
 

In this chapter, we discussed how to implement events in the ASP.NET framework. Event-based programming is a critical aspect of ASP.NET development, making web development more like developing with Visual Basic on the Windows desktop. We also discussed how to bubble events up the control hierarchy and we explored the control life cycle.
 
System.EventHandler is the default delegate type for events in ASP.NET. Inherit from this type when you create custom event handlers so that your controls behave in a similar manner to the built-in controls.
 
Events are generally invoked in a control through a virtual protected method that prefixes the word "On" to the event name to create a method name such as OnClick and OnTextChanged. Custom events implement their own delegate type with the name suffixed by "EventHandler". Custom events can also implement a custom EventArgs-derived class to provide event data tailored to the particular situation. The simplest way for a control to expose an event is to declare one as a public field of a custom control class. Controls can use the Events collection inherited from System.Web.UI.Control to efficiently manage events in a sparse collection instead of the one-field-per-event model. Using the Events collection along with custom event registration code can potentially save a large amount of memory for a control with many events that are not all normally implemented.
 
Command events are a special event type used by list controls in ASP.NET to simplify handling buttons as child controls. Command events expose CommandName and CommandArgument properties to communicate their intentions to the parent control. Event bubbling is a concept in ASP.NET whereby a control can raise an event through its parent control hierarchy. RaiseBubbleEvent starts the event in motion. Parent controls can catch the event by overriding OnBubbleEvent. RaisePostBackEvent is the method in IPostBackEventHandler that allows a control to capture a postback generated by a change in data.
 
INamingContainer is used by a composite control to ensure that its child controls have a unique name on the page even if the composite control is used several times on the page via the UniqueID property. Controls follow a well-defined life cycle execution process to help coordinate events and activities. Understanding the control life cycle will ensure your custom controls behave as expected. The complete control life cycle for an HTTP GET request includes these events in order: Init, TrackViewState, Load, PreRender, SaveViewState, Render, Unload, and Dispose. The complete control life cycle for an HTTP POST request includes these events in order: Init, TrackViewState, LoadViewState, LoadPostData, Load, RaisePostDataChangedEvent, PostBack, PreRender, SaveViewState, Render, Unload, and Dispose.

Total Pages : 12 89101112
Image Loading...