Wednesday, December 21, 2011

Using required field validator with dropdownlist

The requiredfieldvalidator can be used for a dropdown list, you just need to set InitialValue property of the RequiredFieldValidator with the InitialValue of the DropDownList to make it work

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>--Select--</asp:ListItem>
    <asp:ListItem>First Item</asp:ListItem>
    <asp:ListItem>Second Item</asp:ListItem>
    <asp:ListItem>Third Item</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
      ControlToValidate="DropDownList1"
      ErrorMessage="Value Required!"
      InitialValue="--Select--">
</asp:RequiredFieldValidator>


Submit this story to DotNetKicks