69

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

I am unable to validate using a RequiredFieldValidator.

Tony L.
  • 17,638
  • 8
  • 69
  • 66
user242375
  • 691
  • 1
  • 6
  • 5

4 Answers4

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");
Fishcake
  • 10,496
  • 7
  • 44
  • 72
  • HI dude, * this is my code i am still unable to get the message when i submit the button – user242375 Feb 17 '10 at 13:43
  • After databinding does your dropdownlist contain a value for "Selectvalue". I've edited my answer to show how. – Fishcake Feb 17 '10 at 13:56
  • 12
    @Fishcake, you need to have InitialValue="0". Everything else in your answer is good. – strider Sep 11 '13 at 03:25
  • @strider this isn't true. If you follow what I posted and then inspect the generated HTML you get `` and so the InitialValue of "Please select" is correct. – Fishcake Sep 11 '13 at 08:17
  • 6
    If you populated it as follows: `ddl1.Items.Insert(0, new ListItem("Please select", "0"));` then you would indeed need to set `InitialValue = "0"` – Fishcake Sep 11 '13 at 08:39
  • 2
    Instead of insert from codebehind I would add the top item this way: ` ` – ErTR Jul 13 '16 at 14:56
  • Or just leave out the "Please select" item and then you don't need a RequiredFieldValidator because an item will always be selected. – Mike W Jan 14 '19 at 14:03
  • 1
    @MikeW Suddenly 90% of your customer base liives in Afghanistan ;) – Fishcake Jan 14 '19 at 15:06
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" />
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Bhaskar
  • 10,537
  • 6
  • 53
  • 64
  • 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. `` – ferics2 Feb 28 '13 at 20:02
  • 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. – Jujucat Mar 04 '20 at 16:41
  • You forgot the ending `/` at the end of ``. – Pine Code Oct 19 '22 at 07:19
12

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>
Tony L.
  • 17,638
  • 8
  • 69
  • 66
  • 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. – Gorgon_Union Aug 17 '16 at 14:33
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" />
Bhavin
  • 81
  • 1
  • 3