随机更改日历 asp.net C# 中所选日期的背景色

本文关键字:日期 背景色 日历 asp net 随机 | 更新日期: 2023-09-27 18:34:18

使用 Visual Studio for web 2012 中的日历控件,我能够从 SQL Server 2012 数据库中获取日期(即日期和开始日期,并在日历中突出显示此日期(我还能够突出显示结束日期和开始日期之间的日期

总而言之,目前在我的日历中,日历中突出显示了日期 02/10/2013(

至今(和 04/10/2013(开始日期(以及这些日期之间的日期。并且还突出显示了 15/10/2013(至今(和 19/10/2013(开始日期(,并突出显示了这些日期之间的日期。

但是,我希望能够随机更改日历中每个选定日期块的背景颜色?我该怎么做?

非常感谢

这是一些代码,它用背景色突出显示日期并使它们可选等。这段代码运行良好,但我希望能够做到上述操作?

                 protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
                 {
    if (dsHolidays != null)
    {
        foreach (DataRow dr in dsHolidays.Tables[0].Rows)
        {
            DateTime nextDate;
            DateTime endDate;
            nextDate = (DateTime)dr["date"];
            endDate = (DateTime)dr["date1"];
            if (nextDate <= e.Day.Date && endDate >= e.Day.Date)             
            {
                e.Cell.BackColor = System.Drawing.Color.Gray;
                // dates are unselectable
                e.Day.IsSelectable = false;
            }
        }
    }
    // makes the all the first dates selectable 
    foreach (DataRow dr in dsHolidays.Tables[0].Rows)
    {
        DateTime nextDate1;
        nextDate1 = (DateTime)dr["date"];
        {
            if (e.Day.Date == nextDate1)
            {
                e.Day.IsSelectable = true;
                e.Cell.ForeColor = System.Drawing.Color.Blue;
            }
        }
    }
}

随机更改日历 asp.net C# 中所选日期的背景色

也许这段代码可以提供帮助

Random randomGen = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
Color newColor = Color.FromKnownColor(names[randomGen.Next(names.Length)]);
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.IsWeekend)
    {
        e.Day.IsSelectable = false;
        e.Cell.BackColor = System.Drawing.Color.Yellow;
    }
    if(e.Day.Date.Day%2==0 && !e.Day.IsOtherMonth && !e.Day.IsWeekend)
    {
        e.Day.IsSelectable = false;
        e.Cell.BackColor = System.Drawing.Color.Orange;
        e.Cell.ToolTip = "Booked";
    }
    if (e.Day.Date.Day % 2 != 0 && !e.Day.IsOtherMonth && !e.Day.IsWeekend)
    {
        e.Day.IsSelectable = false;
        e.Cell.BackColor = System.Drawing.Color.PaleGreen;
        e.Cell.ToolTip = "Available";
    }
    if(e.Day.Date.Day%5==0 && !e.Day.IsOtherMonth && !e.Day.IsWeekend )
    {
        e.Day.IsSelectable = false;
        e.Cell.BackColor = System.Drawing.Color.Green;
        e.Cell.ToolTip = "Fast Booking";
    }
}