如何使用FindControl查找下拉列表的值?

时间:2017-07-06 11:26:12

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

我正在使用VB.NET和ASP.NET开发一个网站。我目前有重复的DropDownLists供用户提供输入。

设计反复出现。这些DropDownLists从数据库表中获取它们的值,除了将这些重复值写入数据库之外,Everything with the Web interface正在工作 - 这只是为了给你一些背景知识。

我已经设置了每个DropDownList的ID,如下所示:

FrequencyList.ID = String.Concat("FreqList", DBReader(0))

在读取DatabaseReader时,这是一个循环。

这就是我遇到的问题(请注意我简化了代码以便于阅读:

    Dim i As Integer
    DBCommand = New SqlCommand()

    DBCommand.Connection = DBConnection
    DBCommand.CommandType = Data.CommandType.StoredProcedure
    DBCommand.CommandText = "StoredProcedureName"
    DBConnection.Open()
    For i = 1 To AspectTableLength


        Dim ParamFrequencyID As SqlParameter = DBCommand.Parameters.Add("@nFrequencyID", SqlDbType.Int)
        ParamFrequencyID.Value = FindControl("FreqList" & Convert.ToString(i))
        ParamFrequencyID.Direction = ParameterDirection.Input
    Next

FindControl("FreqList" & Convert.ToString(i))变量不正确,因为它无法访问该值 - 并且添加.SelectedItem.Value不起作用。

1 个答案:

答案 0 :(得分:1)

我得到了开发人员的帮助。

Dim MyControls As ControlCollection = Panel.Controls
        Dim Number As Integer 'this is the same as "DBReader(0)"

        For Each MyControl As Control In MyControls
            If MyControl.ID Is Nothing Then
            Else
                If MyControl.ID.StartsWith("Span") Then
                    Number = Replace(MyControl.ID, "Span", "")
                    Dim Freq As DropDownList = PanelMain.FindControl(“FreqList” & Number)

                    Dim ParamFrequencyID As SqlParameter = DBCommand.Parameters.Add("@nFrequencyID", SqlDbType.Int)
                    ParamFrequencyID.Value = Freq.SelectedIndex
                    ParamFrequencyID.Direction = ParameterDirection.Input

                    DBCommand.ExecuteNonQuery()
                    DBCommand.Parameters.Clear()
                End If
            End If
        Next

        DBConnection.Close()