TelerikDeveloper.com

TelerikDeveloper.com

sgray128 7/21/2016 1:30:00 PM

Example code for the ASP Dropdown list

This article contains quick examples of how to bind the ASP Dropdown list control


Bind to a stored proc

Me.ddlVendor.DataSource = SPs.FP_PM00200_SEL3(AppUser.Database).getTable
Me.ddlVendor.DataTextField = "vendname"
Me.ddlVendor.DataValueField = "vendorid"
Me.ddlVendor.DataBind()
Me.ddlVendor.SelectedIndex = -1
Me.ddlVendor.EmptyMessage = "Choose a Vendor"

Bind to static data

Me.ddlItemNumber.Items.Add(New RadComboBoxItem("", ""))
Me.ddlItemNumber.Items.Add(New RadComboBoxItem("Cores", "Cores"))
Me.ddlItemNumber.Items.Add(New RadComboBoxItem("Fuel", "Fuel"))
Me.ddlItemNumber.Items.Add(New RadComboBoxItem("Parts", "Parts"))
Me.ddlItemNumber.Items.Add(New RadComboBoxItem("Supplies", "Supplies"))
Me.ddlItemNumber.EmptyMessage = "Choose an Item"

Tricky way to set the index

Me.ddlBuyer.SelectedIndex = functions.SetIndexByValue(Me.ddlBuyer, Session("buyerID"))
Public Shared Function SetIndexByValue(ByVal ddl As System.Web.UI.WebControls.DropDownList, ByVal strSearch As String) As System.Int16
    Dim i As Integer
    For i = 0 To ddl.Items.Count - 1
        If strSearch = ddl.Items(i).Value Then
            Return i
        End If
    Next
    Return 0
 
End Function