Replace default DNN calendars with AJAX Toolkit Calendar Extender

Although DNN is a very capable CMS, one thing I’d really like to do is send this guy back to 1993:

image

While using a component we purchased (FormMaster), by simply ticking a box, you can call the default DNN calendar for any text field. While this is a cool feature to have it just so happens that the poor ‘ol default DNN Calendar looks like it’s been forgotten about for quite some time.

The first problem you find if you want to put this thing out of its misery is, you can’t. Navigating in the code to “DotNetNuke.Common.Utilities.Calendar” you find the following signature:

“Public Shared Function InvokePopupCal(ByVal Field As System.Web.UI.WebControls.TextBox) As String”. 
Is it a provider: no, so we can't just extend it easily.

One solution, besides modifying the core is to write another module which can ‘hijack’ the DNN Calendars and replace them with the AJAX Toolkit Extender.

Take it or leave it as a solution, here’s what I’ve done.

This piece of code may be considered a little angry, but essentially, it just loops through the controls on the page and looks for Calendar ‘asp:Hyperlinks’.

public static void FindNestedCalendarLinks(ControlCollection current, List<Control> foundCalendars)
{
    foreach (Control c in current)
    {
        if (c is HyperLink && ((HyperLink)c).NavigateUrl.StartsWith("javascript:popupCal('Cal'"))
        {
            foundCalendars.Add(c);
        }
        if (c.Controls != null)
            FindNestedCalendarLinks(c.Controls, foundCalendars);
    }
}

Next, I have a method which is called on the “Page.LoadComplete” event, which adds the AJAX Toolkit Calendar Extender:

public void ReplaceCalendars(ControlCollection controls)
{
    var foundCalendars = new List<Control>();
    FindNestedCalendarLinks(controls, foundCalendars);
    foreach (Control c in foundCalendars)
    {
        string dnnString = ((HyperLink)c).NavigateUrl;

        //extracts the UniqueID: dnn_ctr45232_FormMaster_ctl_e015a64a4a214eaf812836b9f40eb57d
        string textBoxId = matchTextBoxUniqueID.Replace(dnnString, "$1");
        //extracts the original control id: ctl_e015a64a4a214eaf812836b9f40eb57d
        Match textBoxMatch = matchTextBoxOriginalID.Match(textBoxId);

        if (textBoxMatch.Success)
        {
            AjaxControlToolkit.CalendarExtender cal = new AjaxControlToolkit.CalendarExtender();
            c.Parent.Controls.Add((Control)cal);

            //Set properties
            cal.CssClass = CalendarCssClass;
            cal.Format = DateFormat;
            cal.PopupButtonID = c.ID;
            cal.TargetControlID = textBoxMatch.Result("$1");
        }
    }
}

Just for good measure, there’s also a settings page, incase you need to change some of the properties of the Ajax calendars:

image

That’s pretty much the bulk of the work. The end result, a much nicer experience:

image

Downloads

Links

.NET Code Snippets Errors and Bugs ASP.NET
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:15PM

Comments

No comments yet. Be the first!

No new comments are allowed on this post.