views:

411

answers:

1

Hello,

How can I embbed all the scripts needed by the CalendarBehavior in a page, without actually using the server-side control (CalendarExtender). The reason I can't use a server side extender is that I have a page with, possibly, hundreds of Date controls; I want to be able lazy-load the calendars as needed (when the user clicks in the control).

The code that creates the calendar from javascript is the following:

cal = $create(AjaxControlToolkit.CalendarBehavior,
  { 'id': owner.id + '_calendar', 'cssClass': 'calExt', 'format': format }, 
  null, null, owner);

The problem is that without including all the needed js from the AjaxControlToolkit resources, it will throw the error:

AjaxControlToolkit is undefined.

Thank you very much,

Florin S.

A: 

I've found the following hack that will register all the scripts and css references exposed by the CalendarExtender. I think the solution is generic so that it can be used with other extenders:

ScriptManager manager = ScriptManager.GetCurrent(Page);
if (manager != null)
{
  foreach (ScriptReference reference in ScriptObjectBuilder.GetScriptReferences(typeof(CalendarExtender)))
  {
    manager.Scripts.Add(reference);
  }
  CalendarExtender extender = new CalendarExtender();
  Page.Form.Controls.Add(extender);
  ScriptObjectBuilder.RegisterCssReferences(extender);
  Page.Form.Controls.Remove(extender);
}

The temporary extender instance is needed for the RegisterCssReferences call, otherwise it will throw an error.

It works, but YMMV.

Florin Sabau