How to Change Background Color of Current Week in ASP.NET Calendar ?

This tip talks about one interesting customization of ASP.NET Calendar Control. Calendar control provides members like SelectedDayStyle, WeekendDayStyle, TodayDayStyle to apply the styles for specific dates.  Let’s consider a situation where, you need highlight the current week with some different color. ASP.NET Calendar control does not provides any direct styles for the current week selection when your selection mode is date only. Let’s have a look how to deal with that.

First of all we have to find out the current week start date and end date. We can have different logic to implement the same. Here is how I have putted in simple steps,

image

startOfWeek represent the Week Start Date and endOfWeek represent Week End Date. Here, I have consider Week start day as Sunday ( DayofWeek.Sunday).

Once we have the Week start Date and Week End Date, we are pretty good to do some logic in Calendar control DayRender Event. DayRender events in ASP.NET Calendar control is extremely powerful. This can help to customize the calendar control at max level.

image

Below image shows the resulting calendar displays

image

If you look at the code snippet for DayRender event, I have used

image

This will highlight the the date with the same Selected Day Style which falls under current week, other wise you will get the same color ( Lightgreen for the selected date with in the current week )

image

I did this while answering a question at Code Project Q&A forum, http://bit.ly/p9sBgw

Hope this helps !

Cheers !

Abhijit

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.