在一个函数vb.net

时间:2017-05-19 08:45:58

标签: vb.net drop-down-menu

我有初始化下拉列表的功能

    Dim myCommand As OracleCommand = Nothing
    myCommand = _myConnection.CreateCommand()
    myCommand.CommandType = CommandType.Text
    myCommand.CommandText =
        "text"

    ddlStandort.DataSource = myCommand.ExecuteReader()
    ddlStandort.DataTextField = "value"
    ddlStandort.DataValueField = "value"
    ddlStandort.DataBind()

现在我还有四个我要初始化的下拉列表。如何在不重复四次代码的情况下执行此操作?

1 个答案:

答案 0 :(得分:0)

假设每个DDL需要使用完全相同的数据填充,最简单的方法是将该代码转换为接受DropDownList作为参数的方法,然后为每个下拉列表调用该方法。

Private Sub PopulateDDL(byref theDDL as DropDownList)
    Dim myCommand As OracleCommand = Nothing
    myCommand = _myConnection.CreateCommand()
    myCommand.CommandType = CommandType.Text
    myCommand.CommandText = "text"

    theDDL.DataSource = myCommand.ExecuteReader()
    theDDL.DataTextField = "value"
    theDDL.DataValueField = "value"
    theDDL.DataBind()
End Sub

拨打电话

PopulateDDL(ddlStandort)
PopulateDDL(ddlTwo)
PopulateDDL(ddlThree)
...