Reputation: 3563
I'm playing around with the calendar control and I can't seem to do the simple task of shading dates. If the user enters 7 dates I want to shade those dates on the calendar so the user knows they have been selected.
Essentially I want do Calendar.HighlightDate("5/1/11") => imaginary lol I know this must be simple but I'm gong through the properties on MSDN and not finding anything.
Upvotes: 4
Views: 12177
Reputation: 3164
Set the ondayrender event of the calendar object:
<asp:Calendar ID="Calendar1" runat="server" ondayrender="MyDayRenderer">
Then in your code behind, you can check the date and set the color:
protected void MyDayRenderer(object sender, DayRenderEventArgs e)
{
if (e.Day.IsToday)
{
e.Cell.BackColor = System.Drawing.Color.Aqua;
}
if (e.Day.Date == new DateTime(2011,5,1))
{
e.Cell.BackColor = System.Drawing.Color.Beige;
}
}
Upvotes: 6
Reputation: 2542
Here's some code I used on a project a LONG TIME ago. There may be a better way now. But this should work. The easiest way I could find was to dip into the DayRender event.
I used this to highlight certain days that were booked, pending, or available for a rental property.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
for (int x = 0; x < ar.Count; x++)
{
//if the date is in the past, just mark it as booked.
if (e.Day.Date < DateTime.Now)
{
e.Cell.BackColor = System.Drawing.Color.FromArgb(38, 127, 0);
e.Cell.ForeColor = System.Drawing.Color.White;
}
if (e.Day.Date.ToShortDateString() == Convert.ToDateTime(((ListItem)ar[x]).Text).ToShortDateString())
{
switch (((ListItem)ar[x]).Value)
{
case "1":
e.Cell.BackColor = System.Drawing.Color.FromArgb(220,220,220);
break;
case "2":
e.Cell.BackColor = System.Drawing.Color.FromArgb(38,127,0);
e.Cell.ForeColor = System.Drawing.Color.White;
break;
case "3":
if (e.Day.IsWeekend)
{
e.Cell.BackColor = System.Drawing.Color.FromArgb(255,255,204);
}
else
{
e.Cell.BackColor = System.Drawing.Color.White;
}
break;
default:
e.Cell.BackColor = System.Drawing.Color.White;
break;
}
}
}
}
Upvotes: 2