我正在尝试从DetailsView中的绑定文本字段填充日期。当DBNull在列中时,我一直收到错误。填充文本字段时如何避免DBNull。对此的任何帮助都会很有帮助。
我的代码如下:
<asp:TemplateField HeaderText="CBYD Clear Date">
<EditItemTemplate>
<asp:TextBox ID="CBYDExpDate" runat="server" ReadOnly="true" Text='<%# IIf(Eval("CBYDDate") is DBNull.Value,"", String.Format("{0:MM/dd/yyyy}", Eval("CBYDDate").AddDays(30)))%>' />
</EditItemTemplate>
</asp:TemplateField>
答案 0 :(得分:1)
最简单的方法是在你调用的代码中定义一个函数:
Text='<%# getDateText(Eval("CBYDDate"),"{0:MM/dd/yyyy}", 30) %>'
和代码隐藏:
Protected Function getDateText(ByVal value As Object, ByVal dateFormatString As String, ByVal addDays As Int32) As String
If value Is Nothing OrElse value Is DBNull.Value Then
Return String.Empty
ElseIf TypeOf value Is Date Then
Dim d As Date = DirectCast(value, Date)
Return String.Format(dateFormatString, d.AddDays(addDays))
Else
Return value.ToString
End If
End Function
有关其他信息,请查看MSDN