How to change selected date background color of current week in ASP.NET Calendar ?

In previous tip (How to Change Background Color of Current Week in ASP.NET Calendar ? ) we have seen how we can highlight the all dates of current week by changing the background color. This tip is kind of extension of previous tip. Here we will get to know how we can highlight the the Current week selected date in some different color format not Complete Week

To achieve the same, first of all we need to identify the current week first and last date, so that we can put some condition for the specific date range.

The following code demonstrates this technique  :

   #region private members
        /// <summary>
        /// Start Date of Week
        /// </summary>
        private DateTime startOfWeek;

        /// <summary>
        /// End Date of Week
        /// </summary>
        private DateTime endOfWeek;

        /// <summary>
        /// Calculates the days.
        /// </summary>
        private void CalculateDays()
        {
            DayOfWeek day = DateTime.Now.DayOfWeek;
            int days = day - DayOfWeek.Sunday;
            this.startOfWeek = DateTime.Now.AddDays(-days);
            this.endOfWeek = this.startOfWeek.AddDays(6);
        }
        #endregion

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 put the logic on calendar Selection index change. What we will be doing is, whenever the selection date of calendar control falls in between startOfWeek  and endOfWeek  we will change the color style. Below code snippets demonstrates the same:

///

/// Handles the SelectionChanged event of the Calendar1 control.
///

/// The source of the event. /// The instance containing the event data. protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
if (Calendar1.SelectedDate.Date >= startOfWeek.Date && Calendar1.SelectedDate.Date <= endOfWeek.Date) { Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Goldenrod; } else { Calendar1.SelectedDayStyle.BackColor = System.Drawing.Color.Gray; } } } [/code]

Below image shows the resulting calendar displays when selected date is something else the current week.

image

As we have changed the current week selection style, below images shows the resulting calendar display. As, 8th Sept, falls under the current week, the color is something different than what ever we have seen in previous selection date.

image

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.