使用变量设置CType Control ID

时间:2014-02-05 09:33:16

标签: asp.net vb.net

我正在编写一个小的Sub Routine,因此它会动态填充DropDownLists。

但是,当使用变量传递Control ID时,我不确定我是否正确设置了参数。它用于使用24小时时间生成DropDownLists,即sHH

问题在于:

fName = CType(ctrl.FindControl("sHH"), DropDownList)

sHH我需要与fName相同的值。我试图连接:

fName = CType(ctrl.FindControl("'" & sHH & "'"), DropDownList)

这是我在执行上述操作时遇到的错误:

  

运营商'&'没有为类型'String'和'System.Web.UI.WebControls.DropDownList'定义。

开始时间(07至21) 开始分钟(00到45)

结束时间(07至21) 结束分钟(00至45)

我被迫添加:

    Private Property sHH As DropDownList
    Private Property sMM As DropDownList
    Private Property eHH As DropDownList
    Private Property eMM As DropDownList 

这是我的声明:

    'Populate arrays
    Dim ddlHours() As Integer = {"07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21"}
    Dim ddlQuarter() As Integer = {"00", "15", "30", "45"}

    popDDL(sHH, FormView1, ddlHours, 3)
    popDDL(sMM, FormView1, ddlQuarter, 1)
    popDDL(eHH, FormView1, ddlHours, 3)
    popDDL(eMM, FormView1, ddlQuerter, 1)

这是我的Sub Routine:

Public Sub popDDL(ByVal fName As DropDownList, ByRef ctrl As Control, ByVal iDDL() As Integer, Optional ByVal OpLoopTo As Integer = 0)
    'Find the control
    fName = CType(ctrl.FindControl("sHH"), DropDownList)

    'Loop through the length of iDDL items
    For i As Integer = 0 To iDDL.Length - 1

        'Loops through optional value so that I can add leading zeros if necessary
        If OpLoopTo > -1 Then

            Dim j As Integer = 0

            If j < OpLoopTo Then
                j += 1
                'Add Leading zeros to the first values
                fName.Items.Add(iDDL(i).ToString("D2"))
            Else
                'No leading for the remaining
                fName.Items.Add(iDDL(i))
            End If
        Else
            fName.Items.Add(iDDL(i))
        End If

    Next
End Sub

我只做了一个多星期的.NET,我不确定我是否正确设置了参数。

任何想法

1 个答案:

答案 0 :(得分:0)

道歉,这并不困难。我走开了,带着新鲜的眼睛回来。

我创建了一个名为fieldID的附加参数作为字符串

宣言:

popDDL(sHH, "sHH", FormView1, ddlHours, 3)

功能参数和用法:

Public Sub popDDL(ByVal fName As DropDownList, ByRef fieldID As String, ByRef ctrl As Control, ByVal iDDL() As Integer, Optional ByVal OpLoopTo As Integer = 0)
        'Find the control
        fName = CType(ctrl.FindControl(fieldID), DropDownList)

对不起,浪费时间。