Disabling a range of dates on a calendar control is a great way to reduce data-entry error. For example, imagine a scheduling system for an organization whose doors are open Monday through Friday. Ideally, any calendar controls should disable Saturday and Sunday to prevent users from accidentally choosing them. This can be accomplished by taking advantage of the ondayrender event of the calendar control. First, in your aspx markup, wire up a method to the event.
<asp:Calendar Visible="false" ID="DisabledWeekendsCalendar" runat="server" ondayrender="DisabledWeekendsCalendar_DayRender"></asp:Calendar>
Finally, in your .NET code, create the method which checks to see if a day is a weekend, and if so disables it.
protected void DisabledWeekendsCalendar_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.DayOfWeek == DayOfWeek.Saturday || e.Day.Date.DayOfWeek == DayOfWeek.Sunday)
{
e.Day.IsSelectable = false;
e.Cell.ForeColor = System.Drawing.Color.Gray;
}
}
Print | posted on Saturday, April 12, 2008 12:28 PM