INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

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

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

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

(OP)
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?

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

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

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

(OP)
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!

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

glad it helped you!

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

(OP)
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.

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

You need to change the "Type" property to "Integer" for your RangeVaidator

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

(OP)
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?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close