Click here to Skip to main content
Licence 
First Posted 14 Aug 2007
Views 30,594
Downloads 438
Bookmarked 21 times

How to Disable Selected Dates dynamically from the Calendar Control

By prst123 | 14 Aug 2007 | Unedited contribution
Disabling Selected Dates in Calendar
1 vote, 11.1%
1

2
2 votes, 22.2%
3
1 vote, 11.1%
4
5 votes, 55.6%
5
3.29/5 - 10 votes
μ 3.29, σa 2.47 [?]

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

About the Author

prst123

Web Developer

India India

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to close the calendar if choose the same date Pinmember10976918:06 12 Jan '09  
GeneralHi prst123 PinmemberAswanth22:27 3 Jul '08  
GeneralRe: Hi prst123 Pinmemberprst1231:14 28 Jul '08  
GeneralMissing picture PinmvpDavidCrow9:07 14 Aug '07  
GeneralRe: Missing picture Pinmemberprst12322:57 15 Aug '07  

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.

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