How To Add A New Property To The AJAX Calendar Control

I've finally gotten around to using something from the AJAX Control Toolkit in an actual project. My client was dissatisfied with the <asp:calendar> control so I offered to AJAX enable the site and replace it with the fancier Calendar control from the ASP.NET AJAX Control Toolkit. Naturally the client found something he wanted to work a little different. This calendar control was for entering your birth date and he did not want the user to scroll back decades to find that date. He wanted the calendar to show up with the 1990-1999 decade already visible so the user could start from there. Fortunately I remembered that the ASP.NET AJAX Control Toolkit comes with all the source code so you can customize it to suite your needs.

This requirement was extremely easy to implement. All you really need to do is set the calendar mode to "years" when you first show it. However there was no property for initial mode so I had to create one. This was virtually a cut and paste hack because there are existing properties to base it on.

1. Open CalendarExtender.cs and create a new ExtenderControlProperty:

   1: [DefaultValue("")]
   2: [ExtenderControlProperty]
   3: [ClientPropertyName("initialMode")]
   4: public virtual string InitialMode
   5: {
   6:    get { return GetPropertyValue("InitialMode", string.Empty); }
   7:    set { SetPropertyValue("InitialMode", value); }
   8: }

2. Open CalendarBehavior.js and create a new member:

   1: this._initialMode = null;

3. Also in CalendarBehavior.js add new get and set functions for the property access:

   1: get_initialMode : function() { 
   2:     /// <value type="String">
   3:     /// The initial mode of the calendar
   4:     /// </value>
   5:  
   6:     return this._initialMode; 
   7: },
   8: set_initialMode : function(value) { 
   9:     if (this._initialMode != value) {
  10:         this._initialMode = value; 
  11:         this.raisePropertyChanged("initialMode");
  12:     }
  13: }, 

4. And finally set the _switchMode in the show : function() with this new line of code:

   1: this._switchMode(this._initialMode, true);

Compile the AjaxControlToolkit project and copy the AjaxControlToolkit.dll file to your bin directory. Now you can use your new property in the Calendar control like so:

   1: <ajaxToolkit:CalendarExtender ID="calendarButtonExtender" runat="server" TargetControlID="Date5" 
   2:             PopupButtonID="Image1" InitialMode="years" />
Calendar-Years-Mode[4]

5 Comments

  • can you please tell me whether we can any property where it just displays the months instead of dates. I mean, I dont want to display the dates in my ajax calendar control.

  • Interesting theme, I will take part. I know, that together we can come to a right answer.

  • TwellaJep coach factory outlet online
    TotInsuts coach factory online
    guethighsiz coach factory online
    Audisrurn coach outlet online
    Audisrurn

  • I don't create a ton of responses, however I glanced through a few responses on How To Add A New Property To The AJAX Calendar Control - Robert Robbins. I actually do have some questions for you if it's allright.
    Could it be simply me or do some of the comments appear
    as if they are written by brain dead individuals?
    :-P And, if you are posting on other places, I'd like to keep up with anything new you have to post. Could you post a list of all of all your social community sites like your Facebook page, twitter feed, or linkedin profile?

  • You're so interesting! I don't suppose I have read through something like that before.
    So great to find another person with genuine thoughts on this subject.
    Really.. many thanks for starting this up. This site is
    one thing that is required on the web, someone with some originality!

Comments have been disabled for this content.