if(e.Day.Date.DayOfWeek == DayOfWeek.Monday)
{
e.cell.BackColor=System.Drwaing.Color.Red;
}
我正在尝试此代码,但它只更改单个月份的属性,我想更改一年中所有月份的所有 DayOfweek。
最佳答案
您需要使用Calendar.DayStyle
属性设置显示月份中各天的样式属性(包括颜色)。
另请注意,如果您没有为当前显示的月份以外的日期指定不同的样式,这些日期也将使用 DayStyle
指定的样式显示> 属性(property)。
<asp:Calendar id="calendar1" runat="server">
<DayStyle BackColor="Red"></DayStyle>
</asp:Calendar>
如果您希望其他月份的日期以不同的颜色显示,请使用:Calendar.OtherMonthDayStyle
<asp:Calendar id="Calendar1" runat="server">
<OtherMonthDayStyle ForeColor="Green"></OtherMonthDayStyle>
</asp:Calendar>
最后,像往常一样,您也可以在代码中设置颜色属性。在这种情况下使用:Calendar.DayRender
事件。
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;
}
}
最后,在日历中浏览月份时,请使用事件:Calendar.VisibleMonthChanged
,每次用户更改月份时都会引发该值。
标记::
<asp:Calendar ID="Calendar1"
OnVisibleMonthChanged="Calendar1_VisibleMonthChanged" />
代码::
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.Yellow;
Calendar1.DayStyle.BackColor = System.Drawing.Color.Red;
}
关于c# - 如何更改所有月份的所有工作日的颜色属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20833657/