Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How do I change the background color of my asp.net calendar? the future dates only

I have the following code:

C#
txtDateofAccident.Attributes["max"] = DateTime.Now.ToString("yyyy-MM-dd");


I want to set the background color of future dates to red.

for example, today is may 1,

so that dates from may 2 onwards black color is red
Posted

do as below
ASP.NET
<asp:Calendar id="calendar1" 
                    OnDayRender="DayRender"
                    runat="server">

C#
void DayRender(Object source, DayRenderEventArgs e) 
{


 if (e.Day.Date > DateTime.Now.Date)
    e.Cell.BackColor=System.Drawing.Color.Red;

}
 
Share this answer
 
v3
Comments
JB0301 1-May-14 7:13am    
I forgot to tell, this was type = "Date" so it is a textbox and there is no day render event
You have write the code on DayRender event (DamithSL is correct)

C#
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
           if (!e.Day.Date > DateTime.Now.Date)
               e.Cell.BackColor=System.Drawing.Color.Red;
        }
 
Share this answer
 
Comments
JB0301 1-May-14 7:12am    
I forgot to tell, this was type = "Date" so it is a textbox and there is no day render event

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900