asp.net requiredfieldvalidator dont work when visible=false

13,238

Solution 1

Setting Visible="false" in ASP.NET will cause an element to not be rendered out to the page. So, it essentially doesn't exist on the client side. They should be invisible by default, and will only be shown if the criteria of requirements is not met (or they are otherwise forced by use of IsValid="false").

You can play with the Display property to help facilitate layout modes - but I have a feeling this is not entirely related to what you desire to do.

Solution 2

I'm guessing it's because by default the RequiredFieldValidator hides itself by setting the visibility to hidden.

To get it to change display to none instead (which won't leave an empty gap on the page) remove your Visible="False" attribute and add this attribute to the control:

Display="Dynamic"

Solution 3

Try to do this:

$("#myField").hide();

in jQuery, or:

style="display:none"

within the input tag.

Then in will be rendered but just not visible.

Share:
13,238
espvar
Author by

espvar

Updated on June 27, 2022

Comments

  • espvar
    espvar almost 2 years

    I have some requiredFileldvalidators in my asp.net site that i want to set invisible until needed. But when i set them to visible=false they do not fire. They do work if they are set visible=true.

    Is this the correct behavior of this control or is this wrong. I want them invisible due to styling issues when visible.

    • Shai
      Shai about 12 years
      What do you mean set them as invisible? a RequiredFieldValidator does not display a message unless it needs to - i.e unless you press a button that fires validation and it finds that something is missing
    • Rick Hoving
      Rick Hoving about 12 years
      But if there not visible how do you expect them to be valid?
    • espvar
      espvar about 12 years
      The textbox to validate is visible, and i want the fieldvalidator to be invisible until needed. Thats why i have set the property visible=false and the enabled property to true.
    • Shai
      Shai about 12 years
      Why set it as invisible? it is already invisible unless validation goes wrong (i.e textbox is empty once a button is pressed)
  • espvar
    espvar about 12 years
    the css for my validatorcontrol has a red border. So that when visible=true on the validator it appears as a red dott.
  • Shai
    Shai about 12 years
    Good point - there's a walkaround for this issue: set the css display property to none, and then, once required to be visible again (I see absolutely no point in this, as this control is 'invisible' unless validation goes wrong) by settings display to inline - both can be done clientside
  • Sean Kendle
    Sean Kendle over 7 years
    Just use Display="dynamic", that keeps it hidden (display: none) until it's triggered, and then it shows it inline.