c# - 如何更改所有月份的所有工作日的颜色属性

标签 c# asp.net

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/

相关文章:

c# - ASP.net 通过 Ajax 传递 JSON 字符串可以在本地工作,但不能在 Windows Azure Server 上工作

c# - 使用某些货币符号时自定义数字格式和字符串替换期间的意外结果

c# - 在读取带有子对象列表的对象时,需要一个无参数的默认构造函数来允许 dapper 实现错误

asp.net - 散列与加密密码

c# - session 状态奇怪的行为

asp.net - 设置重定向模式时 CustomErrors 不起作用 ="ResponseRewrite"

c# - 用于修复 .NET 应用程序以解决 SQL Server 超时问题并改进执行时间的 list

c# - SharpDX.Direct2D1.Bitmap 的灰度 8BPP 位图

c# - 如何找到哪台计算机发送打印作业?

asp.net - 从主 Web 应用程序自动登录到另一个 ASP.NET 应用程序