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

How to find the value of a dropdownlist using FindControl?

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

设计是重复的。这些 DropDownLists 从数据库 table 获取它们的值,除了将这些重复值写入数据库之外,Web 界面的所有内容都在工作 - 这只是为了给你一些背景知识。

我已经像这样设置了每个 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 不起作用。

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

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