6

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??

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

20

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"
David M
  • 71,481
  • 13
  • 158
  • 186
  • +1 you nailed it - wish I could +100 ! :-) I knew I wasn't seeing the forest for the trees somehow...... Thanks! – marc_s Jun 03 '10 at 11:41
  • You could always bounty it. ;) Probably abuse of the system though. Happy to help. – David M Jun 03 '10 at 11:46
  • 1
    Must be worth a tick at least... ;) – David M Jun 03 '10 at 14:22
  • sure - when I tried to apply it first, I was still in the 10 minute "cannot accept" phase..... but you definitely earned it! – marc_s Jun 04 '10 at 18:13
  • And before anyone else starts banging his head against the wall, make sure you're looking at the correct template (`InsertItemTemplate`/`EditItemTemplate`) if you're using it in a `FormView`. I was pulling my hair out since setting the Type property was not working for me, only because I was fixing it in one template and testing the other. :) – dotNET Oct 02 '14 at 14:52