Extenders are great for providing client functionality to
existing server controls in an incremental way. In many cases, however, we do
not want or do not need an external control to wire client components to a server
control. To describe both the server-side and the client-side functionalities
in a single place, we can resort to script controls. In fact, Script controls
are ASP.NET server controls that can provide script references and script
descriptors without relying on an external object.
Generally, if we are writing the control from scratch, we
can safely derive from the base ScriptControl class,
which takes care of registering the script control with the ScriptManager under
the hood. The only difference between coding the control and coding an extender
is that the properties used to configure the client component and the overrides
of the methods defined in the IScriptControl interface
are embedded in the control rather than in a different object.
In some cases we may want to convert an existing control
into a script control. In such a case we have to derive a class from the
existing server control and manually implement interface IScriptControl.
The following sections will introduce to you the IScriptControl interface.
The IScriptControl Interface and Registering the Script
Control
The IScriptControl interface defines
two methods; Listing 8 and the following Table 3 describe their respective
functionalities:
Listing 8
public interface IScriptControl
{
IEnumerable<ScriptDescriptor> GetScriptDescriptors();
IEnumerable<ScriptReference> GetScriptReferences();
}
Table 3
Method
|
Description
|
GetScriptDescriptors
|
Gets 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
|
Gets 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.
|
Registration with the ScriptManager is necessary in order to
recognize a script control as an Ajax-enabled control. It is a two-step
process similar to that used for extenders.
1. During the PreRender stage, we
call the RegisterScriptControl method, passing the
script control instance as an argument.
2. During the Render stage, you call
the RegisterScriptDescriptors method to register the
script descriptors.
Here we are unwilling to chatter too much about the pure
theories. Thus, let us make room for a simple sample to let it tell every
detail.