Disable dates in CalendarExtender
Hello Friends,
I was using MS Ajax Calender Extender. It was working fine until I faced a case where I wanted to disable all the dates which are lesser than the current date.
After some help through internet I was able to solve the problem.
I modified the CalendarBehavior.js & Calendar.css.
These files can be found under the calendar folder under the AJaxControltoolkit source file.
Following is the solution for the problem.
I made following changes to the CalendarBehavior.js.
- in the function _performLayout add the lines in bold.
_performLayout : function() {
/// <summmary>
/// Updates the various views of the calendar to match the current selected and visible dates
/// </summary>
var elt = this.get_element();
if (!elt) return;
if (!this.get_isInitialized()) return;
if (!this._isOpen) return;
var dtf = Sys.CultureInfo.CurrentCulture.dateTimeFormat;
var selectedDate = this.get_selectedDate();
var visibleDate = this._getEffectiveVisibleDate();
var todaysDate = this.get_todaysDate();
switch (this._mode) {
case “days”:
var firstDayOfWeek = this._getFirstDayOfWeek();
var daysToBacktrack = visibleDate.getDay() – firstDayOfWeek;
if (daysToBacktrack <= 0)
daysToBacktrack += 7;
var startDate = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), visibleDate.getDate() – daysToBacktrack, this._hourOffsetForDst);
var currentDate = startDate;
for (var i = 0; i < 7; i++) {
var dayCell = this._daysTableHeaderRow.cells[i].firstChild;
if (dayCell.firstChild) {
dayCell.removeChild(dayCell.firstChild);
}
dayCell.appendChild(document.createTextNode(dtf.ShortestDayNames[(i + firstDayOfWeek) % 7]));
}
for (var week = 0; week < 6; week ++) {
var weekRow = this._daysBody.rows[week];
for (var dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++) {
var dayCell = weekRow.cells[dayOfWeek].firstChild;
if (dayCell.firstChild) {
dayCell.removeChild(dayCell.firstChild);
}
dayCell.appendChild(document.createTextNode(currentDate.getDate()));
dayCell.title = currentDate.localeFormat(“D”);
dayCell.date = currentDate;
var todaysDate = this.get_todaysDate();
var myTodaysDate = new Date(todaysDate.getFullYear(), todaysDate.getMonth(), todaysDate.getDate(), this._hourOffsetForDst);
var myCurrentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), this._hourOffsetForDst);
$common.removeCssClasses(dayCell.parentNode, [ “ajax__calendar_other”, “ajax__calendar_active”, “ajax__calendar_deactive” ]);
if(myCurrentDate < myTodaysDate){
Sys.UI.DomElement.addCssClass(dayCell.parentNode, “ajax__calendar_deactive”);
}
else{
Sys.UI.DomElement.addCssClass(dayCell.parentNode, this._getCssClass(dayCell.date, ‘d’));
}
currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + 1, this._hourOffsetForDst);
}
}
this._prevArrow.date = new Date(visibleDate.getFullYear(), visibleDate.getMonth() – 1, 1, this._hourOffsetForDst);
this._nextArrow.date = new Date(visibleDate.getFullYear(), visibleDate.getMonth() + 1, 1, this._hourOffsetForDst);
if (this._title.firstChild) {
this._title.removeChild(this._title.firstChild);
}
this._title.appendChild(document.createTextNode(visibleDate.localeFormat(“MMMM, yyyy”)));
this._title.date = visibleDate;
break;
case “months”:
for (var i = 0; i < this._monthsBody.rows.length; i++) {
var row = this._monthsBody.rows[i];
for (var j = 0; j < row.cells.length; j++) {
var cell = row.cells[j].firstChild;
cell.date = new Date(visibleDate.getFullYear(), cell.month, 1, this._hourOffsetForDst);
cell.title = cell.date.localeFormat(“Y”);
$common.removeCssClasses(cell.parentNode, [ “ajax__calendar_other”, “ajax__calendar_active” ]);
Sys.UI.DomElement.addCssClass(cell.parentNode, this._getCssClass(cell.date, ‘M’));
}
}
if (this._title.firstChild) {
this._title.removeChild(this._title.firstChild);
}
this._title.appendChild(document.createTextNode(visibleDate.localeFormat(“yyyy”)));
this._title.date = visibleDate;
this._prevArrow.date = new Date(visibleDate.getFullYear() – 1, 0, 1, this._hourOffsetForDst);
this._nextArrow.date = new Date(visibleDate.getFullYear() + 1, 0, 1, this._hourOffsetForDst);
break;
case “years”:
var minYear = (Math.floor(visibleDate.getFullYear() / 10) * 10);
for (var i = 0; i < this._yearsBody.rows.length; i++) {
var row = this._yearsBody.rows[i];
for (var j = 0; j < row.cells.length; j++) {
var cell = row.cells[j].firstChild;
cell.date = new Date(minYear + cell.year, 0, 1, this._hourOffsetForDst);
if (cell.firstChild) {
cell.removeChild(cell.lastChild);
} else {
cell.appendChild(document.createElement(“br”));
}
cell.appendChild(document.createTextNode(minYear + cell.year));
$common.removeCssClasses(cell.parentNode, [ “ajax__calendar_other”, “ajax__calendar_active” ]);
Sys.UI.DomElement.addCssClass(cell.parentNode, this._getCssClass(cell.date, ‘y’));
}
}
if (this._title.firstChild) {
this._title.removeChild(this._title.firstChild);
}
this._title.appendChild(document.createTextNode(minYear.toString() + “-” + (minYear + 9).toString()));
this._title.date = visibleDate;
this._prevArrow.date = new Date(minYear – 10, 0, 1, this._hourOffsetForDst);
this._nextArrow.date = new Date(minYear + 10, 0, 1, this._hourOffsetForDst);
break;
}
if (this._today.firstChild) {
this._today.removeChild(this._today.firstChild);
}
this._today.appendChild(document.createTextNode(String.format(AjaxControlToolkit.Resources.Calendar_Today, todaysDate.localeFormat(“MMMM d, yyyy”))));
this._today.date = todaysDate;
},
- in the function _cell_onclick add the lines in bold.
_cell_onclick : function(e) {
/// <summary>
/// Handles the click event of a cell
/// </summary>
/// <param name=”e” type=”Sys.UI.DomEvent”>The arguments for the event</param>
e.stopPropagation();
e.preventDefault();
if (!this._enabled) return;
var target = e.target;
var visibleDate = this._getEffectiveVisibleDate();
Sys.UI.DomElement.removeCssClass(target.parentNode, “ajax__calendar_hover”);
var todaysDate = this.get_todaysDate();
switch(target.mode) {
case “prev”:
case “next”:
this._switchMonth(target.date);
break;
case “title”:
switch (this._mode) {
case “days”: this._switchMode(“months”); break;
case “months”: this._switchMode(“years”); break;
}
break;
case “month”:
if (target.month == visibleDate.getMonth()) {
this._switchMode(“days”);
} else {
this._visibleDate = target.date;
this._switchMode(“days”);
}
break;
case “year”:
if (target.date.getFullYear() == visibleDate.getFullYear()) {
this._switchMode(“months”);
} else {
this._visibleDate = target.date;
this._switchMode(“months”);
}
break;
case “day”:
if(target.date > todaysDate) {
this.set_selectedDate(target.date);
this._switchMonth(target.date);
this._blur.post(true);
this.raiseDateSelectionChanged();
}
break;
case “today”:
var todaysTargetDate = target.date;
todaysTargetDate = new Date(todaysTargetDate.getFullYear(), todaysTargetDate.getMonth(), todaysTargetDate.getDate(), this._hourOffsetForDst);
if(todaysTargetDate > todaysDate) {
this.set_selectedDate(target.date);
this._switchMonth(target.date);
this._blur.post(true);
this.raiseDateSelectionChanged();
}
break;
}
},
and in Calendar.css add the following lines:
.ajax__calendar .ajax__calendar_deactive .ajax__calendar_day {background-color:#ffffff;border-color:#ffffff;color:#646464;text-decoration:line-through;}
.ajax__calendar .ajax__calendar_deactive .ajax__calendar_month {background-color:#ffffff;border-color:#ffffff;color:#646464;text-decoration:line-through;}
.ajax__calendar .ajax__calendar_deactive .ajax__calendar_year {background-color:#ffffff;border-color:#ffffff;color:#646464;text-decoration:line-through;}
After making all these changes, rebuild the project & get the dll from the bin folder.
How we do this “rebuild the project & get the dll from the bin folder”
Romain said this on April 24, 2009 at 3:07 pm |
wow!!.I been trying to do something simillar..but never found solution….thanks a lot it works great
ap said this on June 3, 2009 at 9:26 pm |
it works on localhost but when i put the webpage on server its not disabling the dates its just the showing the normal calender.
Please let me know what need to do to make it work at server
Thank you
Singh said this on June 26, 2009 at 5:12 pm |
You simply need put to newly build ajaxtoolkit dll in the bin folder of your sever where your webpage is hosted.
It should work but if it is still not working.
Let me know if I can help you.
gratisaccount said this on June 26, 2009 at 9:52 pm |
i have used your code. but its not displaying the calendar. so please correct me what has wrong. its urgent for me
Angel said this on July 31, 2009 at 6:32 am |
wow! great post!
Jai said this on January 31, 2010 at 2:10 pm |
Hello, I have a similar problem. I want to make enabled only a couple of dates, which are actually stored on the Database. I retreive them on to my page, and on the CalendarExtender, I want only those dates to be “Çlickable’. How can I do that?
Chathu said this on August 29, 2010 at 2:53 pm |
How to call the function from calender behaviour.js file in ajax caleder of aspx page
prasanthi said this on August 31, 2010 at 11:31 am |
This post helps me to understand the calender control .where wil i get the AJaxControltoolkit source file and calender.js .
prasanthi said this on September 9, 2010 at 7:29 am |
Hi,
I want your compiled AjaxControlToolKit library with the disabled option.
If you can, Please upload your library.
Thanks.
Dani said this on March 11, 2011 at 3:20 am |
Hi,
Great post! Sure helped me a lot. One question though, is it possible to override this code so that we can disable or enable the previous dates by passing a property like EnablePrevoiusDates=”false” or “true” at runtime or in Design coding(.aspx) so that one can use multiple calendar extenders on a single page?
Thanks again..
nav said this on April 27, 2011 at 12:17 am |
Hi ,
How to disable dates after particular date,so that user cannot select those dates.
Arjun said this on December 16, 2013 at 11:21 am |