【问题标题】:disable month in calendar thats not in list禁用不在列表中的日历中的月份
【发布时间】:2013-04-26 14:24:01
【问题描述】:

我正在屏幕上显示日历。日历突出显示存储在“日记条目”列表中的日期。 ATM 用户可以使用日历顶部的箭头浏览任何一年中的任何一个月。我只希望用户能够在存储日期的月份之间导航,例如,如果日期是 btn 2013 年 1 月 3 日和 2013 年 4 月 7 日,则用户无法将日历移动到 2013 年 5 月 8 日,如何到达日期时可以禁用箭头键吗?

<asp:Calendar ID="calendarToDisplayWorkSiteDates" VerticalAlign="top" HorizontalAlign="left" runat="server" OnSelectionChanged="LoadRequestedDate_OnClick" OnDayRender="cal_DayRender"></asp:Calendar>

public void BindData()
        {
            DateTime Date = new DateTime(DiaryDate.Year, DiaryDate.Month, 1);
            DateTime startOfMonth = Date.AddMonths(-2);
            DateTime endOfMonth = startOfMonth.AddMonths(5).AddDays(-1);

            int siteId = this.siteId;

            ClarkeDBDataContext db = new ClarkeDBDataContext();
            List<Diary_Entry> DiaryEntry = new List<Diary_Entry>();

            DiaryEntry = (from DE in db.Diary_Entries
                          where DE.Site_Id == siteId
                          && DE.Date >= startOfMonth && DE.Date <= endOfMonth
                          orderby DE.Date ascending
                          select DE).ToList();

            if (DiaryEntry != null)
            {
                FirstDateLabel.Text = DiaryEntry.FirstOrDefault().Date.Date.ToShortDateString();
                SecondDateLabel.Text = DiaryEntry.LastOrDefault().Date.Date.ToShortDateString();

                foreach (DateTime d in DiaryEntry.Select(de => de.Date))
                {
                    calendarToDisplayWorkSiteDates.SelectedDates.Add(d);
                }
            }

            else
            {
                FirstDateLabel.Text = "None";
                SecondDateLabel.Text = "None";
            }


        }

        protected void cal_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsToday)
                e.Cell.BackColor = Color.Red;
            else if (e.Day.Date == this.DiaryDate)
                e.Cell.BackColor = Color.Green;
            else if (e.Day.IsSelected)
                e.Cell.BackColor = Color.Blue;
        }

【问题讨论】:

    标签: asp.net asp.net-mvc-3 calendar


    【解决方案1】:

    我认为不可能(轻松)禁用箭头,最简单的方法是使用DayRender 事件来设置IsSelectable property

    protected void cal_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsToday)
            e.Cell.BackColor = Color.Red;
        else if (e.Day.Date == this.DiaryDate)
            e.Cell.BackColor = Color.Green;
        else if (e.Day.IsSelected)
            e.Cell.BackColor = Color.Blue;
        // adjust accordingly
        if (e.Day.Date < MinDate || e.Day.Date > MaxDate)
        {
            e.Day.IsSelectable = false;
            e.Cell.BackColor = Color.LightGray;
        }
    }
    

    【讨论】:

    • @John:不客气。你可能想接受答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多