Computer
Computer

Reputation: 2227

Highlight selected date in Calendar

I am retrieving some dates from a database and holding them in a List ListDates (going to leave this code out but it shouldnt have any issues with the problem im trying to resolve).

The calendar highlights all days when there is a meeting scheduled.

A user selects a date from an ASP .Net calendar control.

In the DayRender event i have the following code

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (ListDates != null )
    {
        if (ListDates.Contains(e.Day.Date))
        {
            e.Cell.CssClass = "highlight";
        }

        if (ListDates.Contains(Calendar1.SelectedDate))
        {
            e.Cell.CssClass = "newHighlight";
        }
}
}

So at page load i get a list of dates from the db and highlight them in the calendar control (so users know there is a meeting on those days). The code that does that is

        if (ListDates.Contains(e.Day.Date))
        {
            e.Cell.CssClass = "highlight";
        }

The issue i have is when a user selects a date when a meeting is occurring (so this date has the css class highlight applied to it) it defaults to the controls color.

For that reason i decided to add a newHighlight css class so when a user selects a date with a meeting scheduled i would like to give it a new Css class (newHighlight).

To resolve this issue i added this code

        if (ListDates.Contains(Calendar1.SelectedDate))
        {
            e.Cell.CssClass = "newHighlight";
        }

The problem i have is selecting a date with a meeting highlights EVERY cell (the entire calendar) and not just the one cell which the user selected (which also has a meeting on that day)?

Upvotes: 0

Views: 1284

Answers (1)

Dan Dumitru
Dan Dumitru

Reputation: 5423

You probably just need to add a check in the second if:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (ListDates != null )
    {
        if (ListDates.Contains(e.Day.Date))
        {
            e.Cell.CssClass = "highlight";
        }

        if (ListDates.Contains(Calendar1.SelectedDate) && e.Day.Date == Calendar1.SelectedDate)
        {
            e.Cell.CssClass = "newHighlight";
        }
    }
}

Upvotes: 1

Related Questions