user3140153
user3140153

Reputation: 47

how to change color property of all weekdays of all months

if(e.Day.Date.DayOfWeek == DayOfWeek.Monday) { e.cell.BackColor=System.Drwaing.Color.Red; }

I am trying this code but it only changes property of single month, I want to change all DayOfweek in all month in the year.

Upvotes: 2

Views: 694

Answers (1)

R.C
R.C

Reputation: 10565

You need to use the Calendar.DayStyle property to set the style properties ( including color ) for the days in the displayed month.

Also, note that If you do not specify a different style for the dates NOT in the currently displayed month , these dates will also be displayed using the style specified by the DayStyle property.

<asp:Calendar id="calendar1" runat="server">
<DayStyle BackColor="Red"></DayStyle>
</asp:Calendar>

In case you want that the dates of other Months are displayed in a different color use: Calendar.OtherMonthDayStyle

<asp:Calendar id="Calendar1" runat="server">
<OtherMonthDayStyle ForeColor="Green"></OtherMonthDayStyle>
</asp:Calendar>

Lastly, as usual you can also set color properties in code. In this case use : Calendar.DayRender event.

void Calendar1_DayRender(Object sender, DayRenderEventArgs e) 
      {    
         // Change the background color of the days in other Months
         // to yellow.
         if (e.Day.IsOtherMonth)
         {
            e.Cell.BackColor=System.Drawing.Color.Yellow;
         }
         if (!e.Day.IsOtherMonth) //  color to red for current month
         {
            e.Cell.BackColor=System.Drawing.Color.Red;
         }


      }

Finally, when Navigating through Months in a calendar, use event: Calendar.VisibleMonthChanged, which is raised each time the user changes the months.

Markup::

<asp:Calendar ID="Calendar1"
     OnVisibleMonthChanged="Calendar1_VisibleMonthChanged" />

Code::

protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)  
    {  
        Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.Yellow;  
        Calendar1.DayStyle.BackColor = System.Drawing.Color.Red;  
    } 

Upvotes: 2

Related Questions