views:

1329

answers:

1

When I change ddl.Visible=true(it gets changed on partial postback/updatepanel from another form element event)the RequiredFieldValidator will not fire?

NOTE: This is not a question on how to use the RequiredFieldValidator in a normal circumstance. My form has cascading dropdowns that are all dynamically built with their visibility toggled on and off.

<asp:DropDownList ID="ddl" Visible="false" AutoPostBack="True" runat="server">                                                               
</asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="ddl" 
ID="RequiredFieldValidator1" 
runat="server" ErrorMessage="Required"></asp:RequiredFieldValidator>
+2  A: 

You need to set the InitialValue property on your RequiredFieldValidator so that it knows when the value of the DropDownList has changed. For example, on a dropdown with these values:

  • Please Select
  • Dog
  • Cat
  • Bird

You would add this attribute

InitialValue="Please Select"

to your RequiredFieldValidator.

Without knowing if the value has changed it is impossible for the validator to know whether or not the user has satisfied its requirement.

Andrew Hare
InitalValue has a default of String.Empty which is what my first ListItem is. The validators work fine as long as I don't toggle visibility.
rick schott
Can you post all relevant code please? Your question does not show a value set for InitialValue.
Andrew Hare
Andrew you are correct, digging deeper(too much dynamic making my head spin) I found that my default ListItem's value was 0 not String.Empty. I went ahead and set my InitalValue to be explicit though. Thanks
rick schott
Very cool! Glad to hear that it's working now :)
Andrew Hare
@Andrew, why does the validator need to know that a value has changed in order to determine if that value is e.g. null or not?
ProfK