ASP.NET RangeValidator控件古怪与MaximumValue [英] ASP.NET RangeValidator weirdness with MaximumValue

查看:238
本文介绍了ASP.NET RangeValidator控件古怪与MaximumValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些输入字段和校验器pretty简单的ASP.NET页面。一位现场接受双重看起来是这样的:

I have a pretty simple ASP.NET page with some input fields and validators. One field accepting a double looks like this:

<asp:TextBox ID="tbHeight" runat="server" />
<asp:RangeValidator ID="vdHeight" runat="server" 
    ErrorMessage="Height must be a positive number" Text="*" 
    ControlToValidate="tbHeight" MinimumValue="0" Type="Double" />

<击>可正常工作,用户必须输入一个数字> = 0。结果
更新:预期(该项目一些奇怪的错误),毕竟这是行不通的。见注释下面的细节问题的答案

然后我尝试同样为现场接受一个整数:

I then try the same for a field accepting an integer:

<asp:TextBox ID="tbGrossTonnage" runat="server" />
<asp:RangeValidator ID="vdGrossTonnage" runat="server" 
    ErrorMessage="Gross Tonnage must be a positive whole number" Text="*" 
    ControlToValidate="tbGrossTonnage" MinimumValue="0" Type="Integer" />

当加载ASP页面,这给了我以下错误: vdGrossTonnage'的MaximumValue属性'值''不能转换为类型整数

When loading the ASP-page, this gives me the following error: The value '' of the MaximumValue property of 'vdGrossTonnage' cannot be converted to type 'Integer'.

我没有在系统中的任何特定最大值的要求,所以我只是想为默认为 Int32.MaxValue (虽然我有进入2,147,483,647,因为 MaximumValue 似乎并不接受 Int32.MaxValue 不变)。

I don't have any specific max value requirements in the system, so I would just like it to "default" to Int32.MaxValue (although I'd have to enter 2,147,483,647, since MaximumValue doesn't seem to accept the Int32.MaxValue constant).

为什么一个 RangeValidator控件的类型整数将不接受缺少> MaximumValue属性,但该类型的一个双击这是确定?

Why is it that a RangeValidator of the type Integer won't accept a missing MaximumValue property, but for one of the type Double this is ok?

推荐答案

MinimumValue MaximumValue 属性 RangeValidator控件类返回的String.Empty 作为其默认未设置时。

The MinimumValue and MaximumValue properties of the RangeValidator class return string.Empty as their default when not set.

其次,它也发生了转换() BaseCompareValidator 实施保护的方法,它是用于转换字符串属性值,调用 int.Parse() ValidationDataType.Integer 情况。 int.Parse()不喜欢空字符串,并且会抛出异常。

Next, it also happens that the Convert() protected method implemented by BaseCompareValidator, which is used for converting the string properties to values, calls int.Parse() for the ValidationDataType.Integer case. int.Parse() doesn't like the empty string and will throw an exception.

但是, ValidationDataType.Double )的情况下转换(第一调用另一个保护方法, ConvertDouble()(而不是 double.Parse()),并在该方法它的明确地返回一个字符串值0时,检测到一个空字符串,然后该字符串值0后分析,通过 double.Parse() 0D

But, in the case of ValidationDataType.Double, Convert() first calls another protected method, ConvertDouble() (instead of double.Parse()), and in that method it explicitly returns a string value of "0" when an empty string is detected, and that string value "0" later parses, via double.Parse() into 0d.

整数的情况下不会从的String.Empty 的这种映射为0中受益。

The integer case doesn't benefit from such a mapping of string.Empty to "0".

因此​​,dicrepancy。魔鬼在细节和反射是你的朋友。

Hence, the dicrepancy. The devil is in the details, and Reflector is your friend.

这篇关于ASP.NET RangeValidator控件古怪与MaximumValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆