我也遇到过和你一样的问题,但不完全一样,我有这样的情况:
<asp:RangeValidator ID="rw" ErrorMessage="error"
Text="!" ControlToValidate="r" MinimumValue="1 000,00" MaximumValue="1 000 000,00" Type="Currency" CultureInvariantValues="false" runat="server" EnableClientScript="true" />;
我用数据绑定了我的控件,例如 2 000,00,但我遇到了验证错误
但是当我输入一个值 od 2 000,00 时,一切正常。
答案是 CurrencyGroupSeparator 中的空格,我的文化 pl-pl 中有空格,但不是空格“\x0020”,而是非中断空格“\00A0”
我使用反射器进行了一些挖掘,发现令人费解
货币格式检查在 BaseCompareValidator 类的方法中
private static string ConvertCurrency(string text, NumberFormatInfo info)
在代码中有这样一行:
if (currencyGroupSeparator[0] == '\x00a0')
{
currencyGroupSeparator = " ";
}
我将反编译后的代码放到测试项目中并尝试运行,确实代码运行不正常。
ConvertCurrency(10000000.00m.ToString("n"), NumberFormatInfo.CurrentInfo)
返回空值;
为什么有人把它放在那里我不知道,但后来我评论了它,方法已经开始正常工作了。
我们还不能从源代码编译 .net 框架,所以我们可以做的就是将分隔符从非中断空间更改为空格
所以我们的问题的解决方案是:
Thread.CurrentThread.CurrentCulture = new CultureInfo("一些文化
姓名”);
Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyGroupSeparator
= "\x0020"; Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator
= "\x0020";