How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
When you add an ASP.NET Calendar control to a page that might support edit capabilities, you may want to show the existing value in the Calendar control on post-backs. If you look into this, you'll probably notice the SelectedDate property of the control. This sets the date that is is selected in the control. So doing something like this, you might expect that the control would show the day highlighted automatically:
// set the selected date of the calendar controlCalendar1.SelectedDate = DateTime.Now;
But when you run this, you will see that no day is selected like you see here:
If you were to change DateTime.Now to DateTime.Today, it would highlight the correct day. So why?
There are a couple of problems here. First, the DateTime object is exactly that, a Date and a Time. So the Date, it would show 02/20/2009 and the Time is whatever time it is. Right now it is 3:44 PM. So when you use DateTime.Now, it is pulling the current time.
The Calendar control requires the EXACT date of the day you want to show. Meaning that in order to highlight the right day, you have to use 12:00:00 AM for the time. That's why DateTime.Today works. Because it returns the date and the exact time the day started which is 12:00:00 AM.
There is one more thing that you need to do. If you want to show a date that is not in the current month, the Calendar control will not automatically switch to that date. You have to use the VisibleDate property which tells the control what day to make visible. So the final code to set a date correctly would look like this:
// set the selected date of the calendar control
DateTime dateToSet = DateTime.Parse("02/02/2009 12:00:00 AM");Calendar1.SelectedDate = dateToSet;
Calendar1.VisibleDate = dateToSet;
Notice the 12:00:00 AM time was specified so that it will show the date. Now when you run this code, you will see the following:
This will show the day no matter what Date you set the SelectedDate and VisibleDate to.
Popular Articles
Last viewed:
- Using Nullable Data Types with C#
- ASP.NET Charting Control 3.5 fix for "Error executing child request for ChartImg.axd"
- Fix for Firefox click() event issue
- ASP.NET CSS Highlight TextBox on Focus
- Get the list of ODBC data source names programatically using C#
- Create trigger MySQL 5.0 - super privilege required
Recent comments
- Never seen this issue
3 days 5 hours ago - Error in query.ToList()
3 days 8 hours ago - Thanks
3 days 21 hours ago - Thanks
6 days 19 hours ago - To get the data working,
1 week 2 days ago - If I manually change the
1 week 2 days ago - Handling EDM Relationship Metadata
1 week 2 days ago - About the itextsharp version
1 week 2 days ago - Not sure
1 week 4 days ago - Green traffic bars
2 weeks 2 days ago


Nice tip
And just "Today", I learned a new System.Datetime function.