Hello again friends

Now I would like to know how to validate a DropDownList to know if an item was selected or not

Thanks

Dani AI

Generated

Short note: asked for a JavaScript-based check while suggested a server-side handler. A pragmatic pattern is to do a lightweight client-side check to prevent unnecessary postbacks, and still perform a server-side validation in VB.NET as the final authority.

A simple client-side routine (hooked to the submit button) checks the rendered select element and cancels submission if the default placeholder is still selected:

<script type="text/javascript">
function validateDropDown() {
  var ddl = document.getElementById('<%= DropDownList1.ClientID %>');
  if (!ddl) return true;
  if (ddl.selectedIndex <= 0 || ddl.value === '') {
    var err = document.getElementById('ddlError');
    if (err) err.style.display = 'inline';
    return false;
  }
  return true;
}
</script>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return validateDropDown();" />
<span id="ddlError" style="display:none;color:red">Please pick an item.</span>

If preferring built-in validators, use a RequiredFieldValidator and set its InitialValue to whatever the default option's value is (empty string, "0", etc.). That emits client-side script automatically and still validates on postback.

Troubleshooting: ensure the placeholder ListItem has a distinct value that matches the validator InitialValue; use <%= Control.ClientID %> so the script finds the element; ensure CausesValidation is true on the submitting control; when using UpdatePanel, client script still runs but partial-postback behavior may need extra handling. Always re-check the DropDownList selection on the server in VB.NET before trusting the value.

Recommended Answers

All 3 Replies

From javascript or from server side and in what language?(C#,c++,VB)

Thanks for your help.

I´d like to use JavaScript using VB.

Thanks a lot

Hi,

On your DropDownList control you have a property that called "OnSelectedIndexChanged", What you need to do is assign the name of the method you want to call when a value is changed like that:

<asp:DropDownList id="DropDownList1" OnSelectedIndexChanged = "DropDownList_SelectedIndexChanged" runat="server">
                <asp:ListItem Value="1">1</asp:ListItem>
                <asp:ListItem Value="2">2</asp:ListItem>
                <asp:ListItem Value="3">3</asp:ListItem>
                <asp:ListItem Value="4">4</asp:ListItem>
                <asp:ListItem Value="5">5</asp:ListItem>
            </asp:DropDownList>

After you have done this you need to build your code in javascript likt this:

<script runat="server">
    Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
      Select Case DropDownList1.SelectedItem.Value
        Case 1: Label1.Text = "You have selected 1"
        Case 2: Label1.Text = "You have selected 2"
        Case 3: Label1.Text = "You have selected 3"
        Case 4: Label1.Text = "You have selected 4"
        Case 5: Label1.Text = "You have selected 5"
      End Select
    End Sub
</script>

Enjoy!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.