Question

I have the following dropdown in my page

<asp:DropDownList ID="cboEmployerType" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
                       <asp:ListItem Value="Null">-Select-</asp:ListItem>
                       <asp:ListItem Value="E">Employer</asp:ListItem>
                       <asp:ListItem Value="O">OJT Provider</asp:ListItem>
                   </asp:DropDownList>

And a RequiredFieldValidator for it

<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer" Display="None"></asp:RequiredFieldValidator>

But I do not get the Validation Message. What am I missing?

Was it helpful?

Solution

You have taken Display="None" in RequiredFieldValidator take it as

Display="Dynamic"

and take InitialValue="Null"

also assign the same validation group to drop down list

i.e. ValidationGroup="valEmployer"

OTHER TIPS

It works. Just make the following 3 changes:

  1. Either remove Display="None" or use Display="Dynamic" in RequiredFieldValidator

  2. Set ValidationGroup="valEmployer" to dropdown as well as the button for which the validation should occur

  3. Set InitialValue="Null" instead of InitialValue="null" in RequiredFieldValidator with capital "N"

The following is the code

    <asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
                   <asp:ListItem Value="Null">-Select-</asp:ListItem>
                   <asp:ListItem Value="E">Employer</asp:ListItem>
                   <asp:ListItem Value="O">OJT Provider</asp:ListItem>
               </asp:DropDownList>
               <asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer"  Display="Dynamic"></asp:RequiredFieldValidator>
               <br />
    <asp:Button ID="Button1" ValidationGroup="valEmployer" runat="server" Text="Button" />

Hope this helps.

try with InitialValue

InitialValue="Null" Display="Dynamic" 

Remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control and set InitialValue="Null" instead of "null" in RequiredFieldValidator control. Set Display=Dynamic. Try this.

Add ValidationGroup="valEmployer" property in dropdownlist and in the button on click of which validation occurs.

OR

Just remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control.

This is working for me-

  <asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
               <asp:ListItem Value="Null">-Select-</asp:ListItem>
               <asp:ListItem Value="E">Employer</asp:ListItem>
               <asp:ListItem Value="O">OJT Provider</asp:ListItem>
           </asp:DropDownList>
    <asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" ValidationGroup="valEmployer"  runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType"  Display="Dynamic"></asp:RequiredFieldValidator>

Keep ValidationGroup="valEmployer" on which event you want to fire the Validations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top