Programmatically Selecting Items by Value in an ASP.NET DropDownList using VB.NET

This tip shows how to select an item in an ASP.NET DropDownList control programmatically.
  • 10220

The first method to select an item in an ASP.NET DropDownList control is using SelectedIndex property. To find the SelectedIndex of an item, you can use IndexOf method and pass use FindByValue method. Here is the code snippet to select item Mahesh" in the DropDownList.

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Mahesh"))

Alternatively, you can loop through all items of the DropDownList and compare the value of the item and use Selected property to select the matched item.

Private Function SelectCurrentQuarterAndYear(ByVal stringToSelect As String)
        For Each item As ListItem In DropDownList1.Items
            If item.Text = stringToSelect Then
                item.Selected = True
                Exit For
            End If
End Function


More Articles

© 2020 DotNetHeaven. All rights reserved.