Auto Ad Code

Wednesday, August 18, 2010

Disable Next Previous Months (including all dates other than Current Month) in ASP .Net Calendar Control

Often we need ASP .Net calendar control in our webpages. Most of the times we require navigation in between dates of the calendar control but there can be a requirement that when we want that users should not be able to select dates from month other than the current month.
Some people might be thinking in mind that Microsoft has provided the ShowNextPrevMonth property of a calendar control which can be set to false in order to disable the navigation in between months. Ok, this will disable navigation but what about the dates of adjacent months shown in current month?
Following is the output of a normal Calendar control when ShowNextPrevMonth is not set to false.

Note the red circles in above image. July and September will allow you to navigate through other months and red circles below will allow you to select dates of adjacent months.
Ok now we set the ShowNextPrevMonth property to false and see the output. Following is the output in this case.

So setting out the ShowNextPrevMonth property has disabled the July and September links but dates of adjacent months are still there which can be selected as well. How to disable/hide these? Unfortunately there is no such default property provided using which one can disable or hide these dates but fortunately doing this using code is not a complex task. Use following code and this will hide these dates of adjacent months. We will make use of DayRender event of the Calendar control.
====================================
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Calendar1.ShowNextPrevMonth = False
End Sub

Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender
If e.Day.IsOtherMonth Then
e.Cell.Text = ""
End If
End Sub
=====================================

Following is the output of above code

You see there are no navigational links (July, September) and also dates of adjacent months have been hidden.

Hope this will solve your issue.

1 comment: