Disable Weekends in ASP.NET Calendar Control

While using the ASP.NET Calendar Control, a common requirement is to prevent the users from selecting any dates that fall on a weekend (Saturday & Sunday). Here’s how to do so by using the DayRender event of the ASP.NET Calendar control.

<asp:Calendar ID="wkDayCalendar" runat="server"
ondayrender="wkDayCalendar_DayRender" >
</
asp:Calendar>

C#

protected void wkDayCalendar_DayRender(object sender,
DayRenderEventArgs e)
{
e.Day.IsSelectable = !e.Day.IsWeekend;
}

VB.NET

Protected Sub wkDayCalendar_DayRender(ByVal sender As Object,
ByVal e As DayRenderEventArgs)
e.Day.IsSelectable = Not e.Day.IsWeekend
End Sub

OUTPUT

As you can see, the user cannot select any dates from the weekend as it is not selectable any more.

image

1 comment:

  1. Thank you. Can't get any simpler than that for a solution.

    ReplyDelete