2017-05-19 37 views
0

我有函数初始化下拉列表初始化多个下拉列表中的一个功能vb.net

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

现在我有四个下拉列表中,我要初始化。我如何做到这一点,而不重复我的代码四次?

回答

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