CalendarBehavior popup from Javascript

How can I use all the scripts needed for the CalendarBehavior on the page without actually using the server side control ( CalendarExtender ). The reason I am unable to use the server side extender is because I have a page with maybe hundreds of Date controls; I want to be able to lazily load calendars as needed (when the user clicks on 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 required js from the AjaxControlToolkit resources, it throws an error:

AjaxControlToolkit is undefined.

      

Many thanks,

Florin S.

+2


source to share


1 answer


I found the following hack which will register all scripts and css links exposed by CalendarExtender . I think the solution is generic, so it can be used with other extensions:

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);
}

      



A temporary expander instance is needed to call RegisterCssReferences , otherwise it will throw an error.

It works, but YMMV.

+1


source







All Articles