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






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Unknown said...

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