简体   繁体   中英

ASP.NET RangeValidator can't do even the most basic math?

I'm having an issue with my ASP.NET RangeValidator controls.

I want to allow users to enter a discount amount, and this amount must be negative (< $0.00). I want to verify that the amount entered in a textbox is a negative value, so I have this in my page markup:

<asp:TextBox ID="tbxDiscount" runat="server" />
<asp:RangeValidator ID="rvDiscount" runat="server" ControlToValidate="tbxDiscount"
     MinimumValue="0.0" MaximumValue="0.0" EnableClientScript="true" 
     ErrorMessage="Please enter a negative value for a discount" />

and I attempt to set the MinimumValue dynamically in my code before the page gets rendered - to the negative equivalent of my item price. So if the item is $69, I want to set the minimum value to - $69:

rvDiscount.MinimumValue = (-1.0m * Price).ToString();

Trouble is: I keep getting this error message:

The maximum value 0.0 cannot be less than the minimum value -69.00 for rvDiscount

WTF?!?!??! Where I come from, -69 $ IS less than $0 ...... so what's the problem?

And more importantly: what is the solution to the problem??

It's not trying to do maths, it's doing a string comparison because you haven't told it otherwise. Try adding this attribute:

Type="Double"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM