C#中的自定义验证错误 [英] ERROR on Custom Validation in c#
本文介绍了C#中的自定义验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误是:
Error is:
String was not recognized as a valid DateTime.
自定义验证器的功能:
Function of Custom Validator:
protected void chkdate1(object sender, ServerValidateEventArgs e)
{
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
DateTime from = DateTime.ParseExact(txt_from_date.Text, "MM/dd/yyyy HH:mm:ss", theCultureInfo);
DateTime to = DateTime.ParseExact(txt_to_date.Text, "MM/dd/yyyy HH:mm:ss", theCultureInfo);
}
自定义验证为:
Custom Validation is:
<asp:CustomValidator ID="scdt1" runat="server" OnServerValidate="chkdate1" ErrorMessage="To date not less than From date">
推荐答案
字符串未被识别为有效的DateTime.
您尝试将文本转换为日期的方式看起来不正确,并且会收到错误消息.
选择一个:
1.找到要在其中转换的区域性信息.
String was not recognized as a valid DateTime.
The way you are trying to convert text to date looks incorrect and you get the error.
Pick one:
1. Definding the culture info in which you want it to be converted.
string dateString = @"06/26/2012";
DateTime date2 = Convert.ToDateTime(dateString, System.Globalization.CultureInfo.GetCultureInfo("en-GB").DateTimeFormat);
或
2. ParseExact
方法:
Or
2.the ParseExact
method:
string dateString = @"06/26/2012";
DateTime date3 = DateTime.ParseExact(dateString, @"MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
这篇关于C#中的自定义验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文