Events in Server Controls


Every server control inherits five events from the base Control class. The following four events correspond to phases in a control's life cycle that we described in the previous section:

  • Init

  • Load

  • PreRender

  • Unload

In addition, the Control class exposes another event: DataBinding . A control raises the DataBinding event whenever the page developer's code calls the control's DataBind method. We'll describe the DataBinding event in Chapter 16, "Data-Bound Controls."

Events in server controls follow the standard event pattern that we described in Chapter 3, "Component Programming Overview." Each event has an associated virtual method named On < EventName > that invokes the delegates that are attached to the event. If you want to respond to an event in your control, you should override the corresponding On < EventName > method instead of attaching a delegate. When you override an On < EventName > method, you must invoke the corresponding method of the base class so that delegates registered with the event are invoked.

In addition to the events your control inherits from the Control class, you can raise other events from your control. The page framework is based on an event-driven programming model, which enables a page developer to attach handlers that execute in response to events raised by controls. When you expose events from your control, you enable a page developer to respond to actions of your control or to changes in the state of your control. For example, the TextBox control raises a TextChanged event when its Text property changes after a round-trip, and the DataGrid control raises an ItemCreated event for each row (or item) it creates.

Like the events exposed by standard ASP.NET controls, the events that you expose from your control are amenable to design tools. The designer displays all the events of the selected control in the property grid, and a page developer can double-click an event to attach an event handler. You should apply DefaultEventAttribute (analogous to the DefaultPropertyAttribute ) to your control so that a page developer can also double-click a control to attach an event handler to its default event. You will see this attribute applied in the examples shown later in this chapter.

Declarative Syntax for Event Wiring

ASP.NET pages provide a declarative syntax for wiring server-side event handlers on the control's tag:

 <asp:Buttonid="button1"OnClick="button1_Click"Text="Submit" runat="server"/> 

This syntax provides an intuitive model for page developers because it is similar to the syntax for binding a client-side event handler on an HTML tag ( onmouse ­over="client-side script function name " ).

When the page parser parses the .aspx file (as we described in "From Text to Controls" in Chapter 2), it transforms the declarative syntax into code that binds an event handler to an event via an event delegate instance:

 button1.Click+=newEventHandler(this.button1_Click); 

Another point ”which will be of more interest to page developers than to control developers ”is that pages and user controls automatically bind methods named Page_Init , Page_Load , Page_PreRender , and Page_Unload to the corresponding intrinsic events ( Init , Load , PreRender , and UnLoad ).



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net