Home  »  Code Snippets  »  ASP.Net  »

How to use RequireFieldValidator to validate DropDownList in asp.net?

 Posted By : Manoranjan SahooPosted Date : 13/07/2011 11:02:33 AM Category : ASP.Net Points : 3

Sometimes we need to make DropDownList as mandatory field in our web form. It means form must not submit to server it this field is not validated. Here i am going to explain an easy technique to validate dropdownlist. To do so we need to add one RequiredFieldValidator into our web form.

RequiredFieldValidator have one property to validate dropdown list called InitialValue.

We just need to set the above property to work perfectly.

Check below code :

<asp:DropDownList id="DropDownList1" runat="server">
    <asp:ListItem Text ="--Select--" Value ="--Select--" ></asp:ListItem>
    <asp:ListItem Text ="Item1" Value ="Item1" ></asp:ListItem>
    <asp:ListItem Text ="Item2" Value ="Item2" ></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" ForeColor="Red"  ErrorMessage="*" InitialValue="--Select--"></asp:RequiredFieldValidator>

 

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.