asp.net.ph

Skip Navigation Links

Selecting Dates Programmatically

Controls You Can Use on Web Forms   ASP.NET Standard Controls   Calendar Control


You can set date selections in your own code, either a single date or a range of dates. In contrast to user selection in the control on a page, you can select multiple non-sequential dates in code.

NOTE: Setting a date programmatically does not raise the SelectionChanged event.

To select a single date

  • Set the control’s SelectedDate property to an expression of type DateTime.

To select a range of dates

  • Call the Add method of the control’s SelectedDates collection. You can add dates in any order, because the collection will order them for you. The collection also enforces uniqueness and will therefore ignore a date you add if the date is already in the collection.

The following example sets the selection to every Wednesday in the month of February, 2000.

Public Sub Calendar1_SelectionChanged ( ByValControls As Object, ByVal e As EventArgs )
   Calendar1.SelectedDates.Clear
   Calendar1.SelectedDates.Add new System.DateTime ( 2000, 2, 2 )
   Calendar1.SelectedDates.Add new System.DateTime ( 2000, 2, 9 )
   Calendar1.SelectedDates.Add new System.DateTime ( 2000, 2, 16 )
   Calendar1.SelectedDates.Add new System.DateTime ( 2000, 2, 23 )
End Sub

The following example selects a sequence of seven dates.

Dim dt As New DateTime
dt = "01/01/2000"
Calendar1.SelectedDates.Add ( dt )
For i = 1 to 6
   Calendar1.SelectedDates.Add ( dt.AddDays ( i ) )
Next i

To clear a date selection

  • Call the Clear method of the control’s SelectedDates collection, as in the following example:
Calendar1.SelectedDates.Clear

You can also set the SelectedDay property to DateTime.Empty to clear the selection of a single date.

Calendar1.SelectedDay = new System.DateTime.Empty


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note