【问题标题】:asp.net access dynamic controls within a dynamic controlasp.net访问动态控件内的动态控件
【发布时间】:2015-10-12 14:28:03
【问题描述】:

我需要根据在下拉视图中选择的项目构建高级搜索功能。

我已经在 asp.net 中以编程方式构建了一个表格,并且在这个表格中我还动态添加了一个标签、文本框和下拉控件。

这些都渲染得很好,但是我卡住的部分是从文本框和随后的下拉列表中获取值。

这就是表格的构建方式

Public Sub buildAdvancedOptions()

        'Creat the Table and Add it to the Page
        Dim table As New Table()
        table.ID = "advancedTable"
        Page.Form.Controls.Add(table)


        For i As Integer = 0 To _dictOfClassAndCol.Count() - 1
            Dim row As New TableRow()

            Dim labelCell As New TableCell()
            Dim textCell As New TableCell()
            Dim optionsCell As New TableCell()
            Dim tb As New TextBox()
            Dim lbl As New Label()
            Dim ddl As New DropDownList()

            ' Set a unique ID for each TextBox added
            lbl.Text = _dictOfClassAndCol.Keys.ElementAt(i).ToString()
            lbl.ID = "lbl" & _dictOfClassAndCol.Keys.ElementAt(i)
            tb.ID = "txt" & _dictOfClassAndCol.Keys.ElementAt(i)
            ddl.ID = "ddl" & _dictOfClassAndCol.Keys.ElementAt(i)
            ddl.Items.Add("is equal to")
            ddl.Items.Add("is not equal to")
            ddl.Items.Add("is like")
            ddl.Items.Add("is not like")
            ddl.Items.Add("contains")
            ddl.Items.Add("between")

            labelCell.Controls.Add(lbl)
            textCell.Controls.Add(tb)
            optionsCell.Controls.Add(ddl)
            ' Add the TableCell to the TableRow
            row.Cells.Add(labelCell)
            row.Cells.Add(textCell)
            row.Cells.Add(optionsCell)
            row.Attributes.Add("class", _dictOfClassAndCol.Keys.ElementAt(i).ToString().Replace(" ", ""))

            ' Add the TableRow to the Table
            table.Rows.Add(row)
        Next
        advanced.Controls.Add(table)


    End Sub

这就是我试图获取文本框和下拉列表的值的方式,以便我可以将它们传递给另一个函数来构建字符串。

Protected Sub btnAdvancedSearch_Click(sender As Object, e As EventArgs) Handles btnAdvancedSearch.Click

        Dim sb As StringBuilder = New StringBuilder()
        Dim message As String = ""
        For Each control As table In advanced.Controls.OfType(Of table)()                
            message += control.ID + ": " + control.ID  + "\n"
        Next
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)

    End Sub

我可以看到我正在构建正确数量的行和单元格,但无法进入名为"advancedTable" 的表格控件以在此处找到控件。

非常感谢任何有关如何执行此操作的帮助。

【问题讨论】:

    标签: asp.net .net vb.net


    【解决方案1】:

    buildAdvancedOptions() 的开头,表格被添加到Page.Form.Controls,然后在最后你有advanced.Controls.Add(table)。根据按钮单击事件中的代码,我认为不需要将其添加到Page.Form.Controls

    【讨论】:

      【解决方案2】:

      尝试使用FindControl 函数并转换表格:

      Dim tblTarget As Table = CType(advanced.FindControl("advancedTable"), Table)
      
      Dim strMessage As String = String.Empty
      
      For Each objControl As Control In tblTarget.Controls
      
          strMessage += objControl.ID & " / "
      
      Next
      

      需要额外的代码来遍历行/单元格,确定搜索参数控件的类型(DropDown vs TextBox)并获取它们的值,但这至少应该让您掌握表格。

      【讨论】:

      • 很抱歉,Windows 10 BSOD 发生了,我失去了一天的工作,稍后回复您看看这是否有效
      • 你确实将它添加到表单中,所以不妨试试 Page.Form.FindControl("advancedTable) 而不是 advanced.FindControl。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2010-12-02
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多