ASP.NET AJAX RC and ScriptControls

In a previous post, I talked about how to go about creating your own ExtenderControls. Lately, I have been busy creating another control to add into the Ajax Control Toolkit, however this control is based off a ScriptControl.

In previous releases of ASP.NET AJAX, you needed to implement the IScriptControl interface. This meant implementing 2 methods (in similar fashion to extender controls). These methods are the:

IEnumerable<ScriptDescriptor> GetScriptDescriptors();

IEnumerable<ScriptReference> GetScriptReferences();

And also, you needed to provide code similar to the following in the Pre-Render event:

ScriptManager.GetCurrent(Page).RegisterScriptControl<YourScriptControl>(this);

in order to register your control with the ScriptManager control. In the latest RC release, when implementing this interface, you also need to do the following code:

ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);

to also register your ScriptDescriptors and basically register your instance script with associated properties. So your Pre-Render event looks like:

protected override void OnPreRender(EventArgs e)
{
   base.OnPreRender(e);

   ScriptManager.GetCurrent(Page).RegisterScriptControl<YourScriptControl>(this);
   ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
}

While this is required if implementing the IScriptControl interface, the RC release of ASP.NET Ajax also provides a base ScriptControl class that you can inherit from. What this means is that you still have to implement the 2 methods:

IEnumerable<ScriptDescriptor> GetScriptDescriptors();

IEnumerable<ScriptReference> GetScriptReferences();

but you dont have to provide the extra registration steps within the Pre-Render event. So inheriting from the System.Web.UI.ScriptControl class means a little less work, which is always good for someone as lazy as myself.

No Comments