在asp:日历控件上禁用未来日期

时间:2013-10-22 12:09:37

标签: asp.net calendar

如何在asp:calendar控件上停用未来日期?

例如,今天是2013年10月22日,我想禁用10月23日以后的日期, 并在当前日期重新启用它们。

2 个答案:

答案 0 :(得分:2)

您需要使用DayRender方法。

C#代码背后

    public void Calendar1_DayRender(object o, DayRenderEventArgs e)
            {


                e.Cell.BackColor = System.Drawing.Color.Empty;
                e.Cell.ForeColor = System.Drawing.Color.DarkRed;

                //if the days are not part of the current month
                if (e.Day.IsOtherMonth)
                {  
                    e.Cell.Text = "";
                }
             }

然后在日历中,你需要放置

<强> ASP

<asp:Calendar ID="Calendar1" runat="server" Height="145px" Width="77px" OnDayRender="Calendar1_DayRender"></asp:Calendar>

您将OnDayRender =“Calendar1_DayRender”添加到日历控件。

希望这有帮助!

编辑:我误读了。但是如果你只想“启用”当天而不是其他日子,那么这将有效..

代码背后

public void Calendar1_DayRender(object o, DayRenderEventArgs e)
        {


            e.Cell.BackColor = System.Drawing.Color.Empty;
            e.Cell.ForeColor = System.Drawing.Color.DarkRed;

            if (e.Day.IsToday)
            {
                e.Cell.BackColor = System.Drawing.Color.Blue;
            }
            else
            {
                e.Cell.Text = "";
            }
        }

答案 1 :(得分:2)

您可以使用此技巧在Calendar Control中禁用将来的日期。

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{

  if (e.Day.Date > DateTime.Today)
  {

    e.Day.IsSelectable = false;
  }

}