DropDownList RequiredFieldValidator is not working

asp.net, validation

Solution

If you have not allready done it you should have a value that means that you are not selecting anything. Like this:

 Organization.Insert(0,new ListItem("--Select--","0"));

And then you need to tell the `asp:RequiredFieldValidator` to check for a value that you are not allowing. So InitialValue value should be 0. Like this:

<asp:RequiredFieldValidator InitialValue="0"

Then when the `--Select--` option is chosen and you try to submit the page. The `asp:RequiredFieldValidator` will fire.

Edit

I don't like the look of the bit of sql you are showing:

Select Distinct 
    'name' As [OrgCode],'Please Select' As [OrgName] 
FROM [MedOrganization] 
Union All 
SELECT [OrgCode], [OrgName] FROM [MedOrganization]

Not a performance vise good piece of sql code. You can do this instead:

Select
    'name' As [OrgCode],'Please Select' As [OrgName] 
Union All 
SELECT [OrgCode], [OrgName] FROM [MedOrganization]

Why use the `distinct` there? When you can just select like above. Back to the question. You can try having the `InitialValue` set to `name` like this:

<asp:RequiredFieldValidator InitialValue="name"

Problem

I have a DropDownList in my asp.net web page. The RequiredFieldValidator is not working. Thanks for looking at my code. ``` <asp:DropDownList ID="Organization" runat="server" DataSourceID="OrganizationList" DataTextField="OrgName" DataValueField="OrgCode" CausesValidation="True"> </asp:DropDownList> <asp:SqlDataSource ID="OrganizationList" runat="server" ConnectionString="<%$ ConnectionStrings:MembershipDB %>" SelectCommand="Admin_GetOrganizationDropDown" SelectCommandType="StoredProcedure"></asp:SqlDataSource> <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidatorOrg" ControlToValidate="Organization" ErrorMessage="Organization is required." Display="Dynamic" /> ```

Original source