Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 获取动态复选框的值_Asp.net_Vb.net_Webforms - Fatal编程技术网

Asp.net 获取动态复选框的值

Asp.net 获取动态复选框的值,asp.net,vb.net,webforms,Asp.net,Vb.net,Webforms,我根据数据库中的值在VB.Net和.aspx页面中动态创建复选框。为了便于对齐,我将它们放在一个两列的表格中。这部分很好用 Private Async Function InitForEditAsync() As Task Dim docList = Await GetLoanApplicationConfigurationDocs() Dim row = New HtmlTableRow() Dim cell = New HtmlTableCell() Di

我根据数据库中的值在VB.Net和.aspx页面中动态创建复选框。为了便于对齐,我将它们放在一个两列的表格中。这部分很好用

 Private Async Function InitForEditAsync() As Task

    Dim docList = Await GetLoanApplicationConfigurationDocs()
    Dim row = New HtmlTableRow()
    Dim cell = New HtmlTableCell()
    Dim i = 0

    For Each doc In docList

        Dim chkBox = New HtmlInputCheckBox()
        Dim lbl = New Label()

        Dim remainder = i Mod 2

        chkBox.ID = "chkDocId" + doc.Id.ToString
        lbl.Text = doc.DisplayName
        cell.Controls.Add(chkBox)
        cell.Controls.Add(lbl)

        row.Cells.Add(cell)
        cell = New HtmlTableCell()

        If remainder <> 0 OrElse i = docList.Count() - 1 Then
            tblEdit.Rows.Add(row)
            row = New HtmlTableRow()

        End If

        i += 1
    Next
End Function
但复选框不会在控件列表中返回。表是,但在控件集合中探索表对象时,以及尝试此操作时,表对象中没有行:

        For Each chkBox As HtmlInputCheckBox In pnlEdit.Controls.OfType(Of HtmlInputCheckBox)
For Each row As HtmlTableRow In tblEdit.Rows.OfType(Of HtmlTableRow)
如果有帮助的话,这里是UI和创建的HTML的剪贴画:


如有任何建议,我们将不胜感激。提前感谢。

基于我从另一个网站获得的一些想法,我将使用asp:CheckBoxList重写此内容。显然,它像数据网格一样绑定,您可以通过它进行枚举。似乎是我需要的

更新:我发布的所有内容都用五行代码解决了!“cblDocList是我的asp复选框列表,docList是我的ienumerable对象

    cblDocList.RepeatColumns = 2
    cblDocList.DataSource = docList
    cblDocList.DataTextField = "DisplayName"
    cblDocList.DataValueField = "Id"
    cblDocList.DataBind()

您可以通过对每一行和每一个单元格进行循环,或者使用Linq使单元格中只有HtmlInputCheckBox类型的控件。 我已经简化了您的代码,使其能够运行,这里还向您展示了一个实现任务的示例。 希望我能理解:)

    Dim tblEdit As New HtmlTable

    For k As Integer = 0 To 10

        Dim cell = New HtmlTableCell()
        Dim row = New HtmlTableRow()
        Dim chkBox = New HtmlInputCheckBox()

        Dim lbl = New Label()
        Dim remainder = k Mod 2

        chkBox.ID = "chkDocId_" + k.ToString
        chkBox.Checked = remainder = 0
        lbl.Text = "Text indicator of CheckBox nr:" + k.ToString

        cell.Controls.Add(chkBox)
        cell.Controls.Add(lbl)

        row.Cells.Add(cell)

        cell = New HtmlTableCell()
        tblEdit.Rows.Add(row)

    Next

    Dim checkBoxes As IEnumerable(Of HtmlInputCheckBox) =
        (From mRow In tblEdit.Rows).Select(Function(mr)
                                               Dim cb = (From cc In CType(mr, HtmlTableRow).Cells
                                                         Where CType(cc, HtmlTableCell).Controls.OfType(Of HtmlInputCheckBox).Count > 0
                                                         Select CType(cc, HtmlTableCell).Controls.OfType(Of HtmlInputCheckBox)()(0)).FirstOrDefault

                                               Return CType(cb, HtmlInputCheckBox)
                                           End Function).ToList


    For Each checkBox In checkBoxes
        Debug.WriteLine("CheckBox ID: {0}  Checked: {1} ", checkBox.ID, checkBox.Checked)
    Next