5

The following code is used for validating DropDownList control using Custom Validator.

Default1.aspx

<td>
        <asp:DropDownList ID="DDL_Product" runat="server" Height="21px" Width="128px">
             <asp:ListItem>Select</asp:ListItem>
             <asp:ListItem>Nokia</asp:ListItem>
             <asp:ListItem>LG</asp:ListItem>
             <asp:ListItem>Samsung</asp:ListItem>
             <asp:ListItem>sony</asp:ListItem>
             <asp:ListItem>Micromax</asp:ListItem>
             <asp:ListItem>Karbonn</asp:ListItem>
             <asp:ListItem>Apple</asp:ListItem>
         </asp:DropDownList>
    </td>
    <td>
         <asp:CustomValidator ID="cv1" Display="Dynamic" ControlToValidate = "DDL_Product" OnServerValidate="ddl_server" runat="server" ForeColor="Red" ErrorMessage="Please Select the Product"></asp:CustomValidator>
    </td>

Default1.aspx.cs

protected void ddl_server(object sender, ServerValidateEventArgs e)
{
     if (e.Value.selectedIndex <= 0)
     {
        e.IsValid = true;
     }
     else
     {
        e.IsValid = false;
     }
}

The above validation not validating. I don't know how to use this control and validate the DropDownList. Please Correct the error.

3
  • What is it exactly that you want to validate, the DropDownList having a selected value? Commented Jul 15, 2013 at 13:33
  • yes. If the index value cannot change from 0 or its value cannot change from "Select". Commented Jul 15, 2013 at 13:38
  • Then you don't need a CustomeValidator, what you need is a RequiredFieldValidator. Commented Jul 15, 2013 at 13:44

3 Answers 3

8

You should use RequireValidator for this.

1) Add the value for the "Select" item, will be used to validate the initial value:

<asp:DropDownList ID="DDL_Product" runat="server" Height="21px" Width="128px">
       <asp:ListItem Value="0">Select</asp:ListItem>
       /*Rest of items*/
</asp:DropDownList>

2) Then use the RequireValidator like this, comparing the initial value from the DDL:

<asp:RequiredFieldValidator InitialValue="0" 
    ID="rfvDDL_Product" Display="Dynamic" 
    ControlToValidate="DDL_Product"
    runat="server"  Text="*" 
    ErrorMessage="Please Select the Product"
    ForeColor="Red">
</asp:RequiredFieldValidator>

EDIT:

For explanation, from MSDN:

CustomValidator Class

Use the CustomValidator control to provide a user-defined validation function for an input control. The CustomValidator control is a separate control from the input control it validates, which allows you to control where the validation message is displayed.

RequiredFieldValidator Class

Use this control to make an input control a required field. The input control fails validation if its value does not change from the InitialValue property upon losing focus.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks alot.. Its working. May i know the reasong for why using RequiredFieldValidator here and where the Custom Validator are applicable to use.
1

Try adding the property AutoPostBack="true" in the DropDownList.

<asp:DropDownList ID="DDL_Product" runat="server" Height="21px" Width="128px"
                  AutoPostBack="true">

and also if it's only to verify if a value was selected, consider using a RequiredFieldValidator instead.

Comments

0

Simply add ValidateEmptyText="true" to your custom validator as follows to also validate if nothing is selected:

<asp:CustomValidator ID="cv1" Display="Dynamic" 
                     ControlToValidate = "DDL_Product" 
                     OnServerValidate="ddl_server" 
                     runat="server" ForeColor="Red" 
                     ErrorMessage="Please Select the Product"
                     ValidateEmptyText="true">
</asp:CustomValidator>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.