Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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.

<code>

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;

}

}

</code>

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.

Thanks.

License

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

Share

About the Author

prst123
Web Developer
India India
No Biography provided

Comments and Discussions

 
QuestionHow to close the calendar if choose the same date Pinmember10976912-Jan-09 17:06 
GeneralHi prst123 PinmemberAswanth3-Jul-08 21:27 
GeneralRe: Hi prst123 Pinmemberprst12328-Jul-08 0:14 
GeneralMissing picture PinmvpDavidCrow14-Aug-07 8:07 
GeneralRe: Missing picture Pinmemberprst12315-Aug-07 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 14 Aug 2007
Article Copyright 2007 by prst123
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid