I think the control
System.Web.UI.WebControls.Calendar
is defective in this respect. MSDN documentation says that the default value is
DateTime.MinValue
. This is a shame, really. It means this value, otherwise valid, is used in this control is roughly equivalent to "no-value" or "unset" status. This is wrong. Please see:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.selecteddate%28v=vs.110%29.aspx[
^].
Note that you cannot have null for
System.DateTime
type, because it is a
value type, not a
reference type. You could use the
nullable type
System.DateTime?
instead. Too bad, this is not how this control works.
Any practical work-around? Well, you can subclass this control, create a derived class "NullableCalendar". Add additional property of the
System.DateTime?
. Internally, handle the event
SelectionChanged
:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.calendar.selectionchanged%28v=vs.110%29.aspx[
^].
In the handler, when some "real" date is selected, assign your nullable property to
SelectedDate
. This way, you will be able to tell "never selected" status of the control by checking this property for null from the "really selected". Think about some custom rendering (such as background color) which would indicate this "never selected" status visually.
—SA