How to add a RequiredFieldValidator to DropDownList control?
Asked Answered
O

5

69

I have a DropDownList binded with aSqlDataSource to display the values from the database.

I am unable to validate using a RequiredFieldValidator.

Oeillade answered 17/2, 2010 at 12:34 Comment(0)
C
106

For the most part you treat it as if you are validating any other kind of control but use the InitialValue property of the required field validator.

<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist" InitialValue="Please select" ErrorMessage="Please select something" />

Basically what it's saying is that validation will succeed if any other value than the 1 set in InitialValue is selected in the dropdownlist.

If databinding you will need to insert the "Please select" value afterwards as follows

this.ddl1.Items.Insert(0, "Please select");
Chick answered 17/2, 2010 at 13:27 Comment(8)
HI dude, <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage="select any value" InitialValue="Selectvalue" >*</asp:RequiredFieldValidator> this is my code i am still unable to get the message when i submit the buttonOeillade
After databinding does your dropdownlist contain a value for "Selectvalue". I've edited my answer to show how.Chick
@Fishcake, you need to have InitialValue="0". Everything else in your answer is good.Cattalo
@Cattalo this isn't true. If you follow what I posted and then inspect the generated HTML you get <option value="Please select">Please select</option> and so the InitialValue of "Please select" is correct.Chick
If you populated it as follows: ddl1.Items.Insert(0, new ListItem("Please select", "0")); then you would indeed need to set InitialValue = "0"Chick
Instead of insert from codebehind I would add the top item this way: <asp:DropDownList ID="ddlDepo" runat="server" AppendDataBoundItems="true"> <asp:ListItem Text="Please select" Value="0"></asp:ListItem> </asp:DropDownList>Attaway
Or just leave out the "Please select" item and then you don't need a RequiredFieldValidator because an item will always be selected.Lesalesak
@MikeW Suddenly 90% of your customer base liives in Afghanistan ;)Chick
G
31

Suppose your drop down list is:

<asp:DropDownList runat="server" id="ddl">
<asp:ListItem Value="0" text="Select a Value">
....
</asp:DropDownList>

There are two ways:

<asp:RequiredFieldValidator ID="re1" runat="Server" InitialValue="0" />

the 2nd way is to use a compare validator:

<asp:CompareValidator ID="re1" runat="Server" ValueToCompare="0" ControlToCompare="ddl" Operator="Equal" />
Grouchy answered 17/2, 2010 at 13:36 Comment(3)
If data binding, I think this was is still preferred over inserting into your list. You just have to change the DropDownList to support it. <asp:DropDownList runat="server" id="ddl" AppendDataBoundItems="true">Boise
Question: I am getting the ErrorMessage for my dropdown just fine by using the RequiredFieldValidator, however, when I then select from the dropdown, all of the other form classes, like my textboxes' error messages clear out along with the ddl's error message.Palomino
You forgot the ending / at the end of <asp:ListItem ... />.Barbiturism
D
13

If you are using a data source, here's another way to do it without code behind.

Note the following key points:

  • The ListItem of Value="0" is on the source page, not added in code
  • The ListItem in the source will be overwritten if you don't include AppendDataBoundItems="true" in the DropDownList
  • InitialValue="0" tells the validator that this is the value that should fire that validator (as pointed out in other answers)

Example:

<asp:DropDownList ID="ddlType" runat="server" DataSourceID="sdsType"
                  DataValueField="ID" DataTextField="Name" AppendDataBoundItems="true">
    <asp:ListItem Value="0" Text="--Please Select--" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvType" runat="server" ControlToValidate="ddlType" 
                            InitialValue="0" ErrorMessage="Type required"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="sdsType" runat="server" 
                   ConnectionString='<%$ ConnectionStrings:TESTConnectionString %>'
                   SelectCommand="SELECT ID, Name FROM Type"></asp:SqlDataSource>
Determinative answered 2/8, 2016 at 17:43 Comment(1)
Best answer I have come across in my search for setting a initial value for a drop down list that uses an SQL Data Source.Barret
C
1

InitialValue="0" : initial validation will fire when 0th index item is selected in ddl.

<asp:RequiredFieldValidator InitialValue="0" Display="Dynamic" CssClass="error" runat="server" ID="your_id" ValidationGroup="validationgroup" ControlToValidate="your_dropdownlist_id" />
Clip answered 28/8, 2018 at 6:28 Comment(0)
H
0

If you are looking to check with empty string then use the following:

<asp:RequiredFieldValidator ID="REQUIREDFIELDVALIDATOR1" ControlToValidate="ControlId" runat="server" ValidationGroup="groupName" InitialValue="" Display="Dynamic" ErrorMessage="Please select" SetFocusOnError="true"></asp:RequiredFieldValidator>
Hardened answered 6/11, 2023 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.