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

4 Comments

Comments have been disabled for this content.