简体   繁体   English

日历控件 - 以编程方式突出显示日期

[英]Calendar Control - Highlight Dates Programmatically

I'm playing around with the calendar control and I can't seem to do the simple task of shading dates.我正在玩日历控件,我似乎无法完成简单的着色日期任务。 If the user enters 7 dates I want to shade those dates on the calendar so the user knows they have been selected.如果用户输入 7 个日期,我想在日历上遮盖这些日期,以便用户知道它们已被选中。

Essentially I want do Calendar.HighlightDate("5/1/11") => imaginary lol I know this must be simple but I'm gong through the properties on MSDN and not finding anything.基本上我想做 Calendar.HighlightDate("5/1/11") => imaginary 大声笑我知道这一定很简单,但我正在浏览 MSDN 上的属性并没有找到任何东西。

Set the ondayrender event of the calendar object:设置日历 object 的ondayrender事件:

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

Then in your code behind, you can check the date and set the color:然后在后面的代码中,您可以检查日期并设置颜色:

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

Here's some code I used on a project a LONG TIME ago.这是我很久以前在一个项目中使用的一些代码。 There may be a better way now.现在可能有更好的方法。 But this should work.但这应该有效。 The easiest way I could find was to dip into the DayRender event.我能找到的最简单的方法是参与 DayRender 事件。

I used this to highlight certain days that were booked, pending, or available for a rental property.我用它来突出显示已预订、待定或可出租的某些日期。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WinRT XAML工具包中突出显示多个日期-日历控件 - Highlight Multiple Dates in WinRT XAML Toolkit - Calendar Control 如何在asp.net的日历控件中以编程方式选择和取消选择多个日期? - How to programmatically select and deselect multiple dates in calendar control in asp.net? 在Datagridview控件中验证/禁用日历列的日期 - Validation/disabling dates of Calendar Column in Datagridview control 在日历控件中更改某些假期日期的背景 - Change background of some holiday dates in Calendar control 是否可以在WPF日历控件中加粗日期? - Is it possible to bold dates in a WPF Calendar Control? 在ASP.NET日历控件中取消选择日期 - Deselect dates in ASP.NET Calendar Control 如何在日历控件ASP.net中突出显示特定日期? - How to highlight specific days in a calendar control ASP.net? 如何在月份日历控制中突出显示日期? - How can I highlight the dates in month calender control? 根据数据库选择突出显示/禁用ajax日历控件中的特定日期 - Higlight/disable specific dates in ajax calendar control based on database selection 如何在视图模型的Calendar控件中访问选定的日期? - How do I access selected dates in a Calendar control in my viewmodel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM