简体   繁体   English

在一个函数vb.net中初始化多个下拉列表

[英]Initialize multiple drop down lists in one function vb.net

I have function that initializes Drop Down List 我有初始化下拉列表的功能

    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()

Now I have four more Drop Down Lists that I want to initialize. 现在,我还有四个要初始化的下拉列表。 How can i do this without repeating my code four times? 如何在不重复四次代码的情况下执行此操作?

Assuming each DDL needs to be populated with the exact same data, the easiest way is to convert that code into a method that accepts a DropDownList as a parameter, then call that method for each of the dropdownlists. 假设每个DDL都需要填充完全相同的数据,最简单的方法是将该代码转换为接受DropDownList作为参数的方法,然后为每个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

Make calls like 像这样打电话

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM