无法将ListItem添加到DropDownList

时间:2019-07-17 07:28:17

标签: asp.net vb.net drop-down-menu

<asp:DropDownList id="batch_id" style="width:30%" runat="server" AppendDataBoundItems="true">
    <asp:ListItem></asp:ListItem>
</asp:DropDownList>
<%CreateSelect()%>
Public Sub CreateSelect()

    Dim OutScript As String
    Dim getIdList As List(Of String) = ConnectDBGetList("SELECT BATCH_ID FROM TABLE")

    batch_id.Items.Add(New ListItem("abc"))
    For i = 0 To getIdList.Count - 1
        batch_id.Items.Add(New ListItem(getIdList(i)))
        OutScript += String.Format("<option value='{0}'>{0}</option>", getIdList(i)) + Environment.NewLine
    Next
    batch_id.DataBind()
    Response.Write(OutScript)

End Sub

虽然Response.Write(OutScript)可以成功地以预期的输出运行,但是asp:DropDownList的显示只有一个空选项,这意味着batch_id.Items.Add对输出没有任何影响。为什么?

1 个答案:

答案 0 :(得分:0)

没有为下拉列表设置数据源。下面的代码应该可以工作。

Public Sub CreateSelect()

    Dim OutScript As String
    Dim getIdList As List(Of String) = ConnectDBGetList("SELECT BATCH_ID FROM TABLE")

    getIdList.Insert(0, "abc")
    For i = 0 To getIdList.Count - 1
        OutScript += String.Format("<option value='{0}'>{0}</option>", getIdList(i)) + Environment.NewLine
    Next

    batch_id.DataSource = getIdList;
    batch_id.DataBind()
    Response.Write(OutScript)

End Sub