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.
- Set the control’s SelectedDate property to an expression of type DateTime.
- 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
- 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