【问题标题】:Calendar Control - Highlight Dates Programmatically日历控件 - 以编程方式突出显示日期
【发布时间】:2018-12-29 10:38:45
【问题描述】:

我正在使用日历控件,但似乎无法完成简单的日期阴影任务。如果用户输入 7 个日期,我想在日历上遮盖这些日期,以便用户知道它们已被选中。

基本上我想做 Calendar.HighlightDate("5/1/11") => 虚构的大声笑我知道这一定很简单,但我正在浏览 MSDN 上的属性并没有找到任何东西。

【问题讨论】:

    标签: c# asp.net calendar


    【解决方案1】:

    设置日历对象的ondayrender事件:

    <asp:Calendar ID="Calendar1" runat="server" ondayrender="MyDayRenderer">
    

    然后在你的代码后面,你可以检查日期并设置颜色:

       protected void MyDayRenderer(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsToday)
            {
                e.Cell.BackColor = System.Drawing.Color.Aqua;
            }
    
            if (e.Day.Date == new DateTime(2011,5,1))
            {
                e.Cell.BackColor = System.Drawing.Color.Beige;
            }
        }
    

    【讨论】:

    • 对不起,约翰,我登录了,我只是看到了第一个答案,我度过了忙碌的一周
    【解决方案2】:

    这是我很久以前在一个项目中使用的一些代码。现在可能有更好的方法。但这应该有效。我能找到的最简单的方法是参与 DayRender 事件。

    我用它来突出显示已预订、待定或可供出租物业的特定日期。

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        for (int x = 0; x < ar.Count; x++)
        {
            //if the date is in the past, just mark it as booked.
            if (e.Day.Date < DateTime.Now)
            {
                e.Cell.BackColor = System.Drawing.Color.FromArgb(38, 127, 0);
                e.Cell.ForeColor = System.Drawing.Color.White;
            }
    
            if (e.Day.Date.ToShortDateString() == Convert.ToDateTime(((ListItem)ar[x]).Text).ToShortDateString())
            {
                switch (((ListItem)ar[x]).Value)
                { 
                    case "1":
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(220,220,220);
                        break;
                    case "2":
                        e.Cell.BackColor = System.Drawing.Color.FromArgb(38,127,0);
                        e.Cell.ForeColor = System.Drawing.Color.White;
                        break;
                    case "3":
                        if (e.Day.IsWeekend)
                        {
                            e.Cell.BackColor = System.Drawing.Color.FromArgb(255,255,204);
                        }
                        else
                        {
                            e.Cell.BackColor = System.Drawing.Color.White;
                        }
                        break;
                    default:
                        e.Cell.BackColor = System.Drawing.Color.White;
                        break;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多