Wednesday, May 09, 2007

Validating Drop Down List with RequiredFieldValidator

we can validate a drop down list with a required field validator

below is a dropdown with some sample data



<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Newton</asp:ListItem>
<asp:ListItem>Davinci</asp:ListItem>
</asp:DropDownList>


and for the validator i have bound it to the drop down using the control to validate property


<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"
ErrorMessage="Please Select An item for the list" InitialValue="Select"></asp:RequiredFieldValidator>


after that we have to set the InitialValue property of the validator
i have set the value as select...the validator will not allow to select the default value ie "select"

also the InitialValue property is case sencitive we have to give the value correctly

No comments: