如何在asp:calendar
控件上禁用将来的日期?
例如,今天是2013年10月22日,我想禁用10月23日以后的日期,并在当前日期时重新启用它们。
你需要使用 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 = "";
}
}
您可以使用此技巧在日历控件中禁用将来的日期。
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date > DateTime.Today)
{
e.Day.IsSelectable = false;
}
}