Search code examples
asp.netrequiredfieldvalidatorhtml-select

Validating drop down list


I Have a dropdownlist in an aspx page, I am populating the dropdownlist using a datatable. How can i apply a requiredfield validator to this dropdownlist.? Plz help me


Solution

  • You have to set the InitialValue for the selected item's Value that is selected first:

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="DropDownList1" 
        InitialValue="-- Please select --" 
        ErrorMessage="Please select something" />
    
     <asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
         <asp:ListItem>-- Please select --</asp:ListItem>
      </asp:DropDownList>