I have never created bindable controls before, but I would like to make suggestion. How about setting your DateTimeOffset
property to be of type Object
. That way, the property will accept any data types including DBNull.
And once inside the Set
code, check if the value passed is DBNull.Value. If so, create a new empty DataTimeOffset? object and save it in the ViewState.
If non DBNull values, throw error if it cannot be be converted to datetime.
I didn't try this though so I don't know if this will work or not.
################ UPDATED ANSWER ################
My suggestion is, you create 2 properties as follows:
Public Property DateTimeOffset() As DateTimeOffset?
Get
Return DirectCast(ViewState("DTO"), DateTimeOffset?)
End Get
Set(ByVal Value As DateTimeOffset?)
ViewState("DTO") = Value
End Set
End Property
<Bindable(True, BindingDirection.TwoWay)>
Public Property DbDateTimeOffset As Object
Get
Return Me.DateTimeOffset
End Get
Set(value As Object)
If IsDBNull(value) OrElse value Is Nothing Then
Me.DateTimeOffset = New DateTimeOffset?
Else
Me.DateTimeOffset = DirectCast(value, DateTimeOffset?)
End If
End Set
End Property
So in your markup, the binding will be to the DbDateTimeOffset
property:
DbDateTimeOffset='<%# Bind("myDateTimeOffsetField") %>'
While in code behind, you can use the other property to read the property without having to cast.