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:
///
///
/// The source of the event.
/// The
{
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.
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.
Hope this helps !
Cheers !
Abhijit