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

时间:2013-10-08 11:15:59

标签: c# asp.net sql-server-2012

在Visual Studio for it 2012中使用日历控件,我可以从SQL Server 2012数据库中获取日期(即至今从日期开始并突出显示日历中的日期)我还可以突出显示到日期从日期之间的日期。

总之,在我的日历中,我的日期02/10/2013(迄今为止)和04/10/2013(从日期开始)在日历中突出显示日期和这些日期之间的日期。并且还突出显示了2013年10月15日(迄今为止)和2013年10月19日(从日期开始),并突出显示了这些日期之间的日期。

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

非常感谢

这里有一些代码,用背面颜色突出显示日期并使它们可选择等等。这段代码完全正常,但我希望能够做到以上几点?

                 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;

            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

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

Random randomGen = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
Color newColor = Color.FromKnownColor(names[randomGen.Next(names.Length)]);

答案 1 :(得分:0)

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";
    }
}