Required validator is not working for dynamic asp.net dropdown list

I have a asp.net dropdown list that the list is populated by a SQL stored proc. I need to add a required validator to the dropdown list. I have searched on the internet and none of the examples have worked. My code for the asp.net dropdown list is below:
 <asp:DropDownList runat="server" ID="ddlResourceCategories" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="AllCategories" DataTextField="Category"
                                                            DataValueField="ID" OnSelectedIndexChanged="ddlResourceCategories_SelectedIndexChanged">
                                                            <asp:ListItem Value="0">Select Category</asp:ListItem>
                                                        </asp:DropDownList>
                                                        <asp:SqlDataSource ID="AllCategories" runat="server" ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>" SelectCommandType="StoredProcedure"
                                                            SelectCommand="sp_get_AllCategories"></asp:SqlDataSource>

Open in new window

newjeep19Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tapan PattanaikSenior EngineerCommented:
Hi newjeep19,

Please add this below line of code to your code.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="ddlResourceCategories" InitialValue="0" runat="server" ErrorMessage="Please select Category"></asp:RequiredFieldValidator>

Open in new window

Tapan PattanaikSenior EngineerCommented:
Hi newjeep19,

Please find  the full code.

            <asp:DropDownList runat="server" ID="ddlResourceCategories" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="AllCategories" DataTextField="CATEGORY"
                DataValueField="ID" OnSelectedIndexChanged="ddlResourceCategories_SelectedIndexChanged"  >
                <asp:ListItem Value="0">Select Category</asp:ListItem>
            </asp:DropDownList>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="ddlResourceCategories" InitialValue="0" runat="server" ErrorMessage="Please select Category"></asp:RequiredFieldValidator>
            <asp:SqlDataSource ID="AllCategories" runat="server" ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>" SelectCommandType="StoredProcedure"
                                                            SelectCommand="sp_get_AllCategories"></asp:SqlDataSource>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.