如何编程设置一个范围验证最大价值? [英] How to programatically set the Maximum Value on a Range Validator?

查看:188
本文介绍了如何编程设置一个范围验证最大价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个范围验证器上的文本框,以防止有人订购更多产品的比可用。我存储在数据库中可用的金额和我数据绑定的远程验证在数据库中的字段的最大值属性。

I want to set a Range Validator on a text box to prevent someone from ordering more of a product than is available. I have the amount available stored in a database and I databound the maximum value property of the Ranged Validator to the field in the database.

<asp:RangeValidator ID="RangeValidator1" runat="server" 
                    ControlToValidate="tbQuantity" Display="Dynamic" ErrorMessage = "Can't Order More Than Quantity." 
                    ForeColor="Red" MaximumValue='<%# Eval("Quantity") %>' MinimumValue="0"></asp:RangeValidator>



然而,当我调试程序我得到一些意想不到的结果。

However when I debug the program I get some unexpected results.

量为17
和1不会触发错误消息,但2-9不和10-17不会触发,但18岁以上的事情。我猜这事做的事实,这是比较字符串,但我不知道如何将它更改为比较数字。

The quantity is 17. and 1 does not trigger the error message, but 2-9 does and 10-17 doesn't trigger it but 18 and up do. I'm guessing this has something to do with the fact that it is comparing strings but I'm not sure how to change it to comparing numbers.

推荐答案

默认范围验证类型为字符串中,将键入属性整数

The default range validator type is string, change the Type property to Integer

<asp:RangeValidator 
    ID="RangeValidator1" runat="server" 
    ControlToValidate="tbQuantity"
    Display="Dynamic"
    ErrorMessage="Can't Order More Than Quantity." 
    ForeColor="Red"
    MaximumValue='<%# Eval("Quantity") %>'
    MinimumValue="0"
    Type="Integer" /> // <-- add type property of integer

的type属性

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols。 basecomparevalidator.type.aspx

这篇关于如何编程设置一个范围验证最大价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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