我在GridView中有一个控件,它可以转换dateformat从MMddyyyy到ddMMyyyy。页面设计上的错误处理
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%# Eval("Value1") != DBNull.Value ?
(Convert.ToDateTime(Eval("Value1")).ToString("dd/MM/yyyy")) : Eval("Value1") %>'>
</asp:Label>
</ItemTemplate>
这完美的作品,如果它得到日期或空值。
但在我的情况下Value1(绑定字段)可以是一个包含任何内容的字符串。说 - 'garbageStr'。
因此它无法转换为日期并抛出错误。
相反,我想,当它不能转换为DateTime格式显示空或空白值。
有什么办法可以解决这个问题吗?
你为什么不使用DateTime.TryParse – skhurams
http://www.dotnetperls.com/datetime-tryparse – skhurams
感谢@skhurams ...代码编辑 – Rab