C# 文本框上的 asp.net 范围验证器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8826328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:39:36  来源:igfitidea点击:

asp.net range validator on textbox

c#asp.net

提问by Jordan Foreman

I have an asp:textboxwith both required and range validators attached to it, where the code looks like this:

我有一个asp:textbox带有必需和范围验证器的附加到它,代码如下所示:

ASP:

ASP:

<asp:TextBox ID="textBox1" runat="server" CausesValidation="true"></asp:TextBox>
<asp:RangeValidator ID="rangeValidator1" runat="server" ControlToValidate="textBox1" MaximumValue="1" MinimumValue="0"
     ValidationGroup="valid" ForeColor="Red" ErrorMessage="Out of Range" />
<asp:RequiredFieldValidator ID="requiredValidator1" runat="server" ControlToValidate="textBox1"
     ValidationGroup="valid" ForeColor="Red" ErrorMessage="Cannot be blank" />

And when the page is dynamically loaded (after a quick callback), I have code that is supposed to change the MaximumValueof the RangeValidator to more specific value. Here is the code for that:

当页面动态加载时(在快速回调之后),我有代码应该MaximumValue将 RangeValidator 的更改为更具体的值。这是代码:

rangeValidator1.MaximumValue = GetMaxValue(params).ToString();

Now, I have set a breakpoint, and rangeValidator1.MaximumValueis being set correctly, however, when the page loads, and I look at the compiled client side javascript, it appears that the maximum value is still only 1.

现在,我已经设置了一个断点,并且rangeValidator1.MaximumValue设置正确,但是,当页面加载时,我查看编译的客户端 javascript,似乎最大值仍然只有 1。

What confuses me more is that any integer typed in will pass, as long as the first digit is a '1'. So if the maxValue is supposedto be something like "1234567", "1" will match, as will "12345678910". But "2" will not. Nor will "3000" or "46000".

更让我困惑的是,只要第一个数字是“1”,输入的任何整数都会通过。因此,如果 maxValue应该是“1234567”之类的东西,“1”将匹配,“12345678910”也将匹配。但“2”不会。“3000”或“46000”也不会。

Has anyone else had a similar issue with RangeValidators on Textboxes?

有没有其他人对文本框上的 RangeValidators 有类似的问题?

采纳答案by Andrew Barber

The RangeValidator handles validation for multiple types. You should make sure to set the Type to Integer. or what ever is appropriate.

RangeValidator 处理多种类型的验证。您应该确保将类型设置为整数。或者什么是合适的。