get values of dynamic checkboxes

I am dynamically creating checkboxes in VB.Net and an .aspx page, based on values in my db. I’m placing them in a two column table for ease of alignment. this part works fine.

 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

Now I need to retrieve the values without knowing the id’s but am not having any luck. I tried this:

        For Each chkBox As HtmlInputCheckBox In pnlEdit.Controls.OfType(Of HtmlInputCheckBox)

but the checkboxes are not returned in the list of controls. The table is, but there are no rows in the table object when I explored it in the control collection and when I tried this:

For Each row As HtmlTableRow In tblEdit.Rows.OfType(Of HtmlTableRow)

If it will help, here is a Snip of the UI and the HTML that is created:
get values of dynamic checkboxes

Any suggestions are appreciated. Thanks in advance.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Based on some ideas I got from another site, I’m going to rewrite this using the asp:CheckBoxList. apparently it binds like a datagrid and you can enumerate through it. Seems like what i need.

UPDATE: Everything I posted to start was resolved with five lines of code! “cblDocList is my asp CheckboxList and docList is my ienumerable of objects.

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

Method 2

It’s something you can do through a loop for each row and each cell or using Linq to have only cells that have controls of type HtmlInputCheckBox inside.
I have simplified your code to be able run that here also shows you an example to achieve your task. Obviously you must change following your exigences .
Hope I well understood 🙂

    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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x