我有一个非常简单的ASP.NET页面,其中包含一些输入字段和验证器。接受双人的一个领域如下:
<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" />
这按预期工作,用户必须输入一个数字&gt; = 0。
更新:这不会像预期的那样工作(项目中的一些奇怪的错误)。有关详细信息,请参阅以下答案的评论
然后我尝试接受整数的字段:
<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属性的值''无法转换为'Integer'类型。
我在系统中没有任何特定的最大值要求,所以我只想“默认”到Int32.MaxValue
(虽然我必须输入2,147,483,647,因为MaximumValue
没有似乎接受Int32.MaxValue
常数)。
为什么类型RangeValidator
的{{1}}不会接受缺失的Integer
属性,但对于MaximumValue
类型的某个属性,这是否正常?< / p>
答案 0 :(得分:15)
MinimumValue
类的MaximumValue
和RangeValidator
属性在未设置时返回string.Empty
作为默认值。
接下来,由Convert()
实现的BaseCompareValidator
受保护方法(用于将字符串属性转换为值)也会int.Parse()
为ValidationDataType.Integer
案例调用int.Parse()
。 ValidationDataType.Double
不喜欢空字符串,会抛出异常。
但是,在Convert()
的情况下,ConvertDouble()
首先调用另一个受保护的方法double.Parse()
(而不是double.Parse()
),并且在该方法中明确地检测到空字符串时返回字符串值“0”,该字符串值“0”稍后通过0d
解析为string.Empty
。
整数情况不会从{{1}}到“0”的映射中受益。
因此,这种双重性。魔鬼在细节中,Reflector是你的朋友。答案 1 :(得分:1)
在Type =“Double”上保留MaximumValue空白相当于将MaximumValue设置为0.我不确定你的第一个例子是否正常工作。
答案 2 :(得分:0)
最佳做法是为MaximumValue
指定RangeValidator
,即使您的要求没有特别要求。{1}}。在这种情况下,只需使用MaxValue
Type
即可。 MaximumValue
的默认值为String.Empty
。
答案 3 :(得分:0)
只是为了让它更加混乱......
在我的formview中(如果有任何不同,它在更新面板中)我有..
<div class="Width100PercentPadded">
<div class="Width40PercentPadded FloatLeft ClearLeft">
Latitude:
</div>
<div class="Width60PercentPadded FloatLeft">
<asp:TextBox runat="server" ID="txtLatitude" CssClass="Width100Percent" Text='<%# Bind("Latitude") %>' ></asp:TextBox>
<asp:CompareValidator ID="CompareValidatorLatitude" runat="server" ControlToValidate="txtLatitude" Operator="DataTypeCheck" Type="Double" ErrorMessage="Latitude must be numeric" Text="must be numeric" ForeColor="Red" Display="Dynamic"></asp:CompareValidator>
<asp:RangeValidator ID="RangeValidatorLatitude" runat="server" ControlToValidate="txtLatitude" MinimumValue="0" MaximumValue="90" ErrorMessage="Latitude in range 0 to 90" Text="range 0 to 90" ForeColor="Red" Display="Dynamic"></asp:RangeValidator>
</div>
</div>
<div class="Width100PercentPadded">
<div class="Width40PercentPadded FloatLeft ClearLeft">
Longitude:
</div>
<div class="Width60PercentPadded FloatLeft">
<asp:TextBox runat="server" ID="txtLongitude" CssClass="Width100Percent" Text='<%# Bind("Longitude") %>'></asp:TextBox>
<asp:CompareValidator ID="CompareValidatorLongitude" runat="server" ControlToValidate="txtLongitude" Operator="DataTypeCheck" Type="Double" ErrorMessage="Longitude must be numeric" Text="must be numeric" ForeColor="Red" Display="Dynamic"></asp:CompareValidator>
<asp:RangeValidator ID="RangeValidatorLongitude" runat="server" ControlToValidate="txtLongitude" MinimumValue="0" MaximumValue="180" ErrorMessage="Longitude in range 0 to 180" Text="range 0 to 180" ForeColor="Red" Display="Dynamic"></asp:RangeValidator>
</div>
</div>
注意我的纬度和经度验证基本相同,只是经度允许更高的最大值。
运行时,完全按预期验证经度 - 类型必须为数字且值必须介于0和180之间。
纬度类型必须为数字,如果值为负,则范围会失败,但不会检查maxvalue。我可以在文本框中输入数百万的值,验证不会失败。