如何使用asp.net c#使每个星期六的第二个星期变成红色

本文关键字:第二个 红色 星期六 asp 何使用 net | 更新日期: 2023-09-27 18:10:40

我对日历控件有疑问。我不知道如何选择一年中所有星期六的第二个星期。我必须使用日历控件。它的ForeColor应该是红色的。我是否应该使用bool标志来选择星期六的第二周?

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        bool flag = false;
        if (e.Day.Date.DayOfWeek.ToString() == "Saturday")
        {
            flag = true;
            e.Cell.ForeColor = System.Drawing.Color.Red;
        }
        if(!flag)
        {
        }
    }
}

如何使用asp.net c#使每个星期六的第二个星期变成红色

类似如下:

bool IsSecondSaturdayInMonth(DateTime date)
{
    // 2nd Saturday cannot be before the 8th day or after 14th of the month...
    if (date.Day < 8 || date.Day > 14)
    {
      return false;
    }
    return date.DayOfWeek == DayOfWeek.Saturday;
 }

你可以这样使用:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (IsSecondSaturdayInMonth(e.Day.Date))
    {
        e.Cell.ForeColor = System.Drawing.Color.Red;
    }
}

不,您不需要使用flag (bool)变量。根据您的情况(即突出显示所有第二和随后的Sat),使用以下示例代码片段(re:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.dayrender%28v=vs.110%29.aspx)

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.Date.DayOfWeek.ToString() == "Saturday" && e.Day.Date.Day > 7)
    {
        e.Cell.ForeColor = System.Drawing.Color.Red;
    }
}

希望这将帮助。祝好,

 //try this....
        if (!e.Day.IsOtherMonth)//check whether the day is in selected month or not
        {               
            // 2nd Saturday must be before the 15th day and after 7th of the month...,so check the condition
            if (e.Day.Date.DayOfWeek == DayOfWeek.Saturday && e.Day.Date.Day > 7 && e.Day.Date.Day < 15)
            {
                e.Cell.ForeColor = System.Drawing.Color.Red;//set the Day in Red colour
                e.Cell.ToolTip = "Second Saturday";//set the tooltip as 'Second saturday'
            }
        }