Check out JobNimbus - CRM for Contractors and Service Professionals.
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:
- Fix for Firefox click() event issue
- Tutorial for Configuring Silverlight 4, Entity Framework and WCF RIA Services in Separate Component Assemblies (DLL’s)
- Fixing Relative Paths in C# ASP.NET When Using Url Rewriting
- Global.asax Events in IIS 6 and IIS 7 for Static Resources
- Installing an SSL / TLS certificate on Windows Server 2008
- How to back up windows with Bacula
Recent comments
- thank you for sharing
6 hours 3 min ago - Great explanation and more questions
1 day 9 hours ago - Insertion of illegal Element:
4 weeks 4 days ago - Insertion of illegal Element: 32
4 weeks 4 days ago - re "But, this will NOT work."
5 weeks 4 days ago - Unable to cast COM object of t
5 weeks 4 days ago - Saved my life
5 weeks 5 days ago - nice
8 weeks 4 days ago - good article
9 weeks 5 days ago - windows 2008 server backups
11 weeks 4 days ago

highlighting selected date
really very helpful.
thanks
Another 'trick'
Dim DefaultDate = Now.AddDays(90)
DefaultDate -= DefaultDate.TimeOfDay
calDateExpected.SelectedDate = DefaultDate
calDateExpected.VisibleDate = DefaultDate
Subtracting the time of day will bring just the date.
YES
Oh my gosh thank you! You are a life saver!
Why have I had to waste 2 hours looking for a solution to this, come on microsoft sort it out, at least be consistent with your controls.
Calendar date time
Thank you this was very helpful. The syntex and examples made it easy to understand and utilize.
Thanks for the tip! This
Thanks for the tip! This helped me out today.
The Date.Property of DateTime can be helpfull, too
Assuming you get some date from anywhere you can jsut say
calendarRouteDay.SelectedDate = myVariableContainingTheDateTime.Date;
calendarRouteDay.VisibleDate = myVariableContainingTheDateTime.Date;
Nice tip
And just "Today", I learned a new System.Datetime function.