Click here to Skip to main content
11,930,449 members (60,610 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked

How to Disable Selected Dates dynamically from the Calendar Control

, 14 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Disabling Selected Dates in Calendar


The calendar control is part of the dot net web controls collection. Its in the namespace System.Web.UI.WebControls. It provides an efficient mechanism for the user to to select dates in the desired format.But one of the functionality that we require most of the time, as part of validation is to disable a range of dates. Mostly we weould like to disable past dates so that user wont select them. This can be done with a little bit of tweaking. I will explain this in my article.

The DayRender event :

The most important event for us in this scenario, is the OnDayRender event. This event fires when the calendar is first rendered on the screen. This happens as a loop, wherein it continuously renders each day, one a time.

We have to write our logic in the OnDayRender event hanlder, to disable rendering when the day matches our criteria.

The Code:

In my code, I have a calendar control. I want to block it for all past days as well as next 7 days. I set this value in a local variable,nDaysToBlock.

As you can see from the code, all the action happens in the DayRender event handler.


protected void myDayRenderMethod(object sener, DayRenderEventArgs e)


if (e.Day.Date < (System.DateTime.Now.AddDays(_nDaysToBlock)))


e.Day.IsSelectable = false;

e.Cell.Font.Strikeout = true;




Here, iam checking if the day being rendered is less than the current day plus number of days to block. i.e, is it less than 7 days from now. In that case, we want to block such a day from being rendered, which is done by making e.Day.IsSelectable as false;

That is all there is to it. You have the desired dates disabled. You can change the font to strike out, so as to easily distinguish them.

The calendar will look as below at runtime, with selected dates disabled.

<formulas /></formulas />

Hope my code snippet was of help to you.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to close the calendar if choose the same date Pin
10976912-Jan-09 18:06
member10976912-Jan-09 18:06 
GeneralHi prst123 Pin
Aswanth3-Jul-08 22:27
memberAswanth3-Jul-08 22:27 
GeneralRe: Hi prst123 Pin
prst12328-Jul-08 1:14
memberprst12328-Jul-08 1:14 
GeneralMissing picture Pin
DavidCrow14-Aug-07 9:07
mvpDavidCrow14-Aug-07 9:07 
GeneralRe: Missing picture Pin
prst12315-Aug-07 22:57
memberprst12315-Aug-07 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 14 Aug 2007
Article Copyright 2007 by prst123
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid