We have already known that an extender’s goal is to wire a
client component to an existing server control. Therefore, we have to make
clear how the client functionality is attached to the extended control.
The easiest way to build an extender is to declare a class
that inherits from the base ExtenderControl class. This
class implements an interface called IExtenderControl
and takes care of registering the extender with the ScriptManager
control. A derived class should override the methods defined in the IExtenderControl interface. Let us look more closely into
this interface before developing our first extender.
The IExtenderControl Interface and Registering the
Extender
The IExtenderControl interface defines the contract to which
a class adheres to become an extender. Listing 3 shows the methods implemented
by the interface and Table 1 gives the detailed explanations.
Listing 3
public interface IExtenderControl
{
IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl);
IEnumerable<ScriptReference> GetScriptReferences();
}
Table
1
Method
|
Functionality
|
GetScriptDescriptors
|
Returns a collection of script descriptors that represent
JavaScript components on the client. This method is used to map server-side
control properties to client-side control properties.
|
GetScriptReferences
|
Returns a collection of ScriptReference objects that
define script resources required by the control. This method is used to
identify one or more embedded script resources in a control assembly and
output them to the client.
|
The process of registering with the ScriptManager lets the
extender be recognized as an Ajax-enabled control. Concretely, this is a
two-step process:
1 During the PreRender stage, we will
call RegisterExtenderControl method, passing the extender instance and the
extended control as arguments.
2 During the Render stage, we will
call the RegisterScriptDescriptors method to register the script descriptors.
The first part of the registration procedure involves
calling the RegisterExtenderControl method on the ScriptManager control. This
method receives the current extender instance and the extended control as arguments.
The registration procedure is completed during the Render phase, where you call
the RegisterScriptDescriptors method on the ScriptManager, passing the current
extender instance as an argument.
Happily, the ExtenderControl class takes care of performing
the registration procedure automatically. Since we always create a new extender
by deriving from the ExtenderControl class, we do not need to worry about the
implementation details. As for script controls to be discussed later, however,
we will discover that in some situations we need to manually register the Ajax-enabled
control with the ScriptManager.
In the following section, we will create an extender for the
MyHoverBehavior behavior we built in earlier article "Explorer Ways to extend ASP.NET AJAX Client-side Function." This will let you wire the
behavior to an ASP.NET Panel control (you can of course try other types of
controls) and configure it on the server side.