How to Use CompareValidator with DropDownList in ASP.NET (VB.NET)

September 07, 2025
ASP.NET VB.NET DropDownList validation example using CompareValidator with error message highlighted in red on a web form.

Validating a DropDownList in ASP.NET Web Forms is a common requirement when you want to make sure users don’t leave the selection at a default placeholder like "-- Select --". One of the simplest ways to achieve this is by using the CompareValidator control

In this guide, you’ll learn how to use CompareValidator with DropDownList in ASP.NET (VB.NET), add styling with SCSS for a better UI, and also ensure server-side validation for security. By the end, you’ll have a fully working example with best practices.

Why Use CompareValidator for DropDownList?

The ASP.NET CompareValidator is typically used to compare values of two inputs. However, it can also validate a single DropDownList value by checking if the selected value is not equal to the default placeholder (e.g., Value=0).

Benefits:

  • No need for custom JavaScript or server-side code for simple validations.
  • Provides real-time feedback when the user submits the form.
  • Improves accessibility and user experience.
  • Works with ASP.NET Web Forms validation framework seamlessly.

DropDownList Validation Process Flow

Here’s how the validation process works:

1
User Interaction:
The user selects an option from the DropDownList.
2
Validation Triggered:
On form submission, the CompareValidator checks if the selection is equal to the placeholder (e.g., Value=0).
3
Validation Outcome:
If invalid → an error message is displayed and focus returns to the DropDownList.
If valid → form submission continues.
5
Server-Side Validation:
The server re-validates for security and processes the input.
5
Result:
FData is saved (or further processed), and the user sees a success message.

Step-by-Step Implementation

Add the DropDownList and CompareValidator

Here’s the ASPX markup example:


<div class="form">
    <div class="form_controls_group">
        <div class="item1">
            <label>Customer Name</label>
        </div>
        <div class="item1">
            <asp:DropDownList ID="DdlCustomerName" runat="server" Width="200px">
                <asp:ListItem Text="-- Select --" Value="0" Selected="True"></asp:ListItem>
                <asp:ListItem Text="John Doe" Value="1"></asp:ListItem>
                <asp:ListItem Text="Jane Smith" Value="2"></asp:ListItem>
            </asp:DropDownList>
        </div>
        <div class="item1">
            <asp:CompareValidator ID="CompareValidator2" runat="server"
                ControlToValidate="DdlCustomerName" Display="Dynamic" Operator="NotEqual"
                ValueToCompare="0" ErrorMessage="Please select a Customer name."
                CssClass="validation" SetFocusOnError="True"></asp:CompareValidator>
        </div>
    </div>

    <div class="form_controls_group">
        <div class="item1">
            <asp:Button ID="BtnSubmit" runat="server" Text="Submit" />
        </div>
    </div>
</div>

Explanation:

  • The DropDownList has a default placeholder ("-- Select --") with Value="0".
  • The CompareValidator ensures the selected value is NotEqual to 0.
  • If validation fails, the user sees the error message.

Apply SCSS Styling (Optional)

You can style the validation message and form using SCSS:


<style>
.form {
    .form_controls_group {
        margin-bottom: 15px;

        .item1 {
            margin-bottom: 10px;

            label {
                font-weight: bold;
                color: #333;
            }

            .validation {
                color: red;
                font-size: 12px;
                margin-left: 10px;
            }
        }
    }
}
</style>

This ensures:

  • Labels are bold and clear.
  • Validation errors are highlighted in red.
  • Clean, modern UI.

Add Server-Side Validation in VB.NET

Even though CompareValidator handles client-side validation, always include server-side checks for security:


Protected Sub BtnSubmit_Click(sender As Object, e As EventArgs) Handles BtnSubmit.Click
    If Page.IsValid Then
        Dim selectedValue As String = DdlCustomerName.SelectedValue
        ' Perform desired actions, such as saving data to the database
        Response.Write("Customer Selected: " & selectedValue)
    End If
End Sub
  • Page.IsValid ensures all validators pass before executing business logic.
  • DdlCustomerName.SelectedValue retrieves the selected option.

Frequently Asked Questions (FAQ)

1. Why is my CompareValidator not working with DropDownList?

  • Make sure ControlToValidate is set to your DropDownList’s ID.
  • Confirm that ValueToCompare matches the placeholder value (e.g., "0").

2. Can I use other validators instead?

  • Yes
  • RequiredFieldValidator – Ensures a non-default value is selected.
  • CustomValidator – Allows complex conditions.

3. What about client-side validation?

  • You can use jQuery or JavaScript to validate instantly.
  • ASP.NET also handles server-side validation if JavaScript is disabled.

4. How do I show all error messages in one place?

  • Use the ValidationSummary control to display multiple validation errors together.

Conclusion

Using CompareValidator with DropDownList in ASP.NET VB.NET is one of the simplest and most effective ways to ensure users select a valid option instead of leaving the default "-- Select --".

By combining client-side validation, SCSS styling, and server-side checks, you create user-friendly, secure, and robust forms.

Try this approach in your next ASP.NET Web Forms project and improve your form validation instantly.

If you found this article helpful, don’t forget to share it with your developer community!

Post ID: 1