Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make a TextBox RangeValidator with a MaximumValue set to a Label?

Status
Not open for further replies.

weberm

Programmer
Dec 23, 2002
240
0
0
US
I have an ASP Label which shows the total number of items in an inventory system that is available for requisitions.

I am working on a way to check the value entered in a TextBox is less than or equal to the number in the Label, which is loaded from an ORACLE database.

I created a RangeValidator but am having trouble with setting the MaximumValue to the contents of the Label. I initially tried to use the Label ID for the parameter but as I understand it, it doesn't work because the two controls are not compatable so I tried to use an expression to get the Text property from the Label but am getting type casting exceptions.

Code:
<asp:TextBox Width="50px" ID="txtQuantity" runat="server">0</asp:TextBox>
<asp:RangeValidator Display="Dynamic" 
     MinimumValue="0" MaximumValue="<%lblAvailableQuantity.text %>" 
     Type="string" ControlToValidate="txtQuantity" 
     ErrorMessage="Quantity is greater than Available" 
     runat="server">*</asp:RangeValidator>
<asp:Label ID="lblAvailableQuantity" runat="server"></asp:Label>

I think I'm on the right track but can't find anything via Google.
Any ideas?

 
I would not mix server code with HTML mark up
Simply set the values in page load. Also set your Type on the range validator to Integer

Page_Load

yourRangeValidator.MinimumValue = 0
yourRangeValidator.MaximumValue = lblAvailableQuantity.Text
 
OK, I removed Maximum value from the server code and set it in Page_Load:
Code:
Label lblActualQuantity = (Label)repItem.FindControl("lblActualQuantity");
RangeValidator valQuantity = (RangeValidator)repItem.FindControl("valQuantity");

valQuantity.MaximumValue = lblActualQuantity.Text;
This seems to have done the trick.
Thanks!
 
One more related question:
Is this validation sppsd to detect if something not numeric is entered? I thought it would since it wouldn't fall in range, but it seems to ignore them in my example. I ask b/c this validator was originally a CompareValidator that tests for integers GreaterThanEqual to zero and it complains about alphanumerics, so I assumed a RangeValidator would behave similarly.
 
You need to change the "Type" property to "Integer" for your RangeVaidator
 
I did that. It acts like it only tests if the field has a numeric value. Weird, eh? OTOH, maybe testing in the Page_load is triggering a Postback?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top