Conditionally Hide some days in the Calendar control

You may wonder how to hide some days in the ASP.NET standard calendar control. for example if you want to hide the week end days ( Saturday and sunday) , you can simply handle the DayRender event which is called for every day in the calendar control :

    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
If e.Day.IsWeekend Then
e.Cell.Controls.Clear()
End If
End Sub


and you may want to hide the days that is older than the current day :

    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
If e.Day.Date.Date < DateTime.Now.Date Then
e.Cell.Controls.Clear()
End If
End Sub

Also Instead of hiding the days ,you can just make them un Selectables :

    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
If e.Day.Date.Date < DateTime.Now.Date Then
e.Day.IsSelectable = False
e.Cell.ToolTip = "sorry , You can't select this date ! "
End If
End Sub

The Calendar control is a good choice when you want to filter some records based on the selected date in the calendar ( when the postback is required ) , but in the the Places where you just want to provide a date selector and there is no need for the postback , Its better to use the Caneldar Extendar ajax toolkit control .

Hope it help

Anas Ghanem

Published Monday, July 28, 2008 10:04 PM by anas
Filed under:

Comments

# re: Conditionally Hide some days in the Calendar control

Tuesday, July 29, 2008 2:10 AM by kamii47

Nice One Anas

# re: Conditionally Hide some days in the Calendar control

Monday, December 15, 2008 10:36 AM by Luis

Thaaaaaaanks it was very helpfull !!!

# re: Conditionally Hide some days in the Calendar control

Monday, January 18, 2010 2:07 AM by Pankaj

thanks it helps a lot..............

# re: Conditionally Hide some days in the Calendar control

Friday, June 17, 2011 5:23 AM by ChellaMuthuRaja

Thank's , very simple to understand.

Thank's a lot !!

Leave a Comment

(required) 
(required) 
(optional)
(required)