Search code examples
.netasp.netvalidationrequiredfieldvalidator

RequiredFieldValidator not firing when ControlToValidate visiblity is changed


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>       

Solution

  • 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.