Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am trying to populate a DropDownList with an Array with what is shown in the List, A, and its value, B.

ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" />

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim A(10), B(10) As String
        A(0) = "this"
        A(1) = "that"
        A(2) = "something else"
        A(3) = "this else"
        A(4) = "that else"
        A(5) = "something something else"
        B(0) = "1"
        B(1) = "2"
        B(2) = "3"
        B(4) = "4"
        B(5) = "5"
        'populate DropDownList1 with A
        DropDownList1.DataSource = A
        DropDownList1.DataTextField = A
        DropDownList1.DataValueField = B
        DropDownList1.DataBind()
    End Sub
Posted

1 solution

You can populate a DropDownList like this way.

VB
Protected Sub Button1_Click(sender As Object, e As EventArgs)

    Dim ItemList As IList(Of Object) = New List(Of Object)
    ItemList.Add(New With {.A = "this", .B = "1"})
    ItemList.Add(New With {.A = "that", .B = "2"})
    ItemList.Add(New With {.A = "something else", .B = "3"})
    ItemList.Add(New With {.A = "this else", .B = "4"})        
    ItemList.Add(New With {.A = "something something else", .B = "5"})

    'populate DropDownList1 with A
    DropDownList1.DataSource = ItemList
    DropDownList1.DataTextField = "A"
    DropDownList1.DataValueField = "B"
    DropDownList1.DataBind()

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900