如何在ASP.net VB中动态创建多个DropDownList

时间:2017-02-17 07:54:01

标签: asp.net vb.net

我想要显示多个DropDownLists,具体取决于BulletedList中的项目。我尝试了以下代码。

Dim quantity As New DropDownList

    For Each x As ListItem In BulletedList1.Items

        quantity.ID = "mylist"
        quantity.Items.Add(New ListItem("1", "1"))
        quantity.Items.Add(New ListItem("2", "2"))
        quantity.Items.Add(New ListItem("3", "3"))
        quantity.Items.Add(New ListItem("4", "4"))
        quantity.Items.Add(New ListItem("5", "5"))
        PlaceHolder1.Controls.Add(quantity)
    Next

1 个答案:

答案 0 :(得分:0)

每个下拉列表都需要唯一ID,您还需要在循环中创建下拉列表。

Dim counter As Integer = 0
For Each x As ListItem In BulletedList1.Items
    counter += 1        
    Dim quantity As New DropDownList
    quantity.ID = "mylist" & counter
    quantity.Items.Add(New ListItem("1", "1"))
    quantity.Items.Add(New ListItem("2", "2"))
    quantity.Items.Add(New ListItem("3", "3"))
    quantity.Items.Add(New ListItem("4", "4"))
    quantity.Items.Add(New ListItem("5", "5"))
    PlaceHolder1.Controls.Add(quantity)
Next