I have been struggling a couple of hours to get the calendar control in
.Net to meet my requirements. What I needed was a control to select a
date, so a DateTimePicker would be great. This does not exist in the
ASP framework, so I needed to use the calendar control or buy some
third party tool.
My requirements was that only dates within a
given time frame should be clickable. The next and previous month
button should also only be clickable if there were clickable dates in
these months. The solution came to me in a dream, no actually a
colleague got me on the idea. The answer was in the events.
Heres the code:
private void calDueDate_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e) {
DateTime dt = Convert.ToDateTime( dueDate );
if( dt <= DateTime.Today.AddDays(30) ){
e.Day.IsSelectable = false;
}
}
private void calDueDate_VisibleMonthChanged(object sender, System.Web.UI.WebControls.MonthChangedEventArgs e) {
DateTime dt = Convert.ToDateTime( pageTask.DueDate);
if( dt <= DateTime.Today){
calDueDate.NextMonthText= ""
calDueDate.NextMonthText= ">";
}
else{
calDueDate.NextMonthText = "";
calDueDate.PrevMonthText = "<";
}
}
private void calDueDate_Load(object sender, System.EventArgs e) {
calDueDate.PrevMonthText = "";
}I must say, the AJAX Calendar looks
pretty sweet as well:
http://ajax.asp.net/ajaxtoolkit/Calendar/Calendar.aspx