Another path to extend ASP.NET AJAX client-side functionalities
is via script controls. Script controls are WebControls
that can provide script references and script descriptors (i.e. implementing IScriptControl) without relying on an external object (like
the first means). We can describe both the server-side and the client-side functionalities
in a single place.
In general, if we want to develop the control from scratch, we
can safely derive from the base ScriptControl class,
which takes the responsibility for registering the script control with the
ScriptManager under the hood. Joyfully, programming the control is pretty similar
to doing an extender.
One point to note is that the IScriptControl
interface, similar to interface IExtenderControl, must be implemented by every
script control. Another point to note is that a script control does not have a
target control; this is why the RegisterScriptDescriptors
method (also to be discussed later) does not receive a reference to the
extended control as happened with extenders.
To make clearer the relationships between the objects
mentioned above, we list the class definition for class ScriptControl
in Listing 2 and give a hierarchical tree between the associated objects in
Figure 2.
Listing 2 - The class definition for class
ScriptControl
public abstract class ScriptControl: WebControl, IScriptControl
{
// Fields
private IScriptManagerInternal _scriptManager;
// Methods
protected ScriptControl();
internal ScriptControl(IScriptManagerInternal scriptManager);
protected abstract IEnumerable < ScriptDescriptor > GetScriptDescriptors();
protected abstract IEnumerable < ScriptReference > GetScriptReferences();
protected override void OnPreRender(EventArgs e);
protected override void Render(HtmlTextWriter writer);
IEnumerable < ScriptDescriptor > IScriptControl.GetScriptDescriptors();
IEnumerable < ScriptReference > IScriptControl.GetScriptReferences();
// Properties
private IScriptManagerInternal ScriptManager
{
get;
}
}
Figure 2 - The hierarchical relationships related
to ScriptControl
Now, with a general profile in mind, let us examine
carefully the above two solutions.