2009-03-03 66 views

回答

2

做的DateTime.TryParse()的答案是伟大的,如果你在你的代码背后做这种检查,但如果您正在捕获的UI这个数据,那么我会非常reccomend validing的输入以及回发到后面的代码发生之前。

<strong>Date:</strong> 
<asp:textbox ID="txtEnterDate" runat="server"></asp:textbox> 
<asp:CompareValidator ID="cvEnterDate" runat="server" ControlToValidate="txtEnterDate" 
    ErrorMessage="Must Be Valid Date" Operator="DataTypeCheck" 
    SetFocusOnError="True" Type="Date"></asp:CompareValidator> 
0
protected void txtTo_TextChanged(object sender, EventArgs e) 
    { 
     TextBox tbTo = (TextBox)sender; 
     DateTime wsToOUT; 
     if (DateTime.TryParse(tbTo.Text, out wsToOUT)) 
     { 
      //do something with valid date in tbTo 
     } 
     else 
     { 
      //show a nice error message 
      tbTo.Focus(); 
     } 
    } 
相关问题