Dynamic CheckBoxes and TextBoxes

Problem Description :

I have Test types in SQL database.
For example
Table name :- tableTest ( testID, TestDesc)
Data in tableTest
100 - Test 1, 200 - Test 2, 300 - Test 3, 400 - Test 4 and so on......
Where 100,200 etc are testID and
Test1, Test2 etc are testDesc

I am showing these tests on Web data input form as check boxes as user can select more than one test types.
i am using checkboxlist to populate tests from tableTest
I take all the tests from the test box and make a string. then convert string to array and load the array in checkboxlist

        Dim strPerTestTypes As String
        strPerTestTypes = "Per"
        Dim myDataReaderTestDetailsPer As SqlClient.SqlDataReader = SqlCommandPerTestDetails.ExecuteReader()
        While myDataReaderTestDetailsPer.Read
            strPerTestTypes = strPerTestTypes + "," + myDataReaderTestDetailsPer.Item("testdesc")
        End While
        Dim arrPerTest() As String = Split(strPerTestTypes, ",")
        Dim i As Integer
        For i = 1 To UBound(arrPerTest)
        perCheckBoxList.RepeatColumns = "6"

This way i get all the checkboxes that i want.
The problem is that i need textboxes associated with every check box.
The user is supposed to enter comments for every test selected.
for example
If the user selects Test 1 check box, he will write comments against test 1 check box. ( in the text box)

Checkbox Test 1  checked .. comments in the corresponding textbox "Test at full load"
Check box Test 2 checked .. comments in the corresponding text box "Test for leaks"

Number of text boxes should be exactly same as check boxes and should be lined right next to check box .

User is forced to fill the text box if the check box is checked. ( for every checked checkbox, there has to be comments in the corresponding text box)

Once the user submits form, i have to collect all the data and save in database.

Thanks in advance

Who is Participating?
Don't use the CheckBoxList for this.  Let's assume you will show the controls in a table; each row will have a cell for the checkbox and a cell for the related textbox.  Add a table (within your server side FORM tag):

<table id="tblTest" runat="server" border="1" width="750">
          <td width="100">Check Box</td>
          <td>Text Box</td>

Note that you have an 'id="..."' and 'runat="server"' attributes on the table tag.  This will allow you to dynamically add to the table in your code.

You already have your data into a data reader and are iterating through the datareader with your While... End While block.  So in that iteration, do this:

' declare an integer variable outside of the While...End While block to keep count of the the record in the datareader.
' increment it here before you have added the row because you already have a header row in the table.

' create table row.
Dim tr as New HtmlTableRow()

' create table cell.
Dim td1 as New HtmlTableCell()

' create checkbox.
Dim cb As New CheckBox()
cb.ID = "cbName_" + recIdx.ToString()
cb.Text = "Some text"

' add checkbox to table cell.

' create table cell.
Dim td2 As New HtmlTableCell()

' create textbox.
Dim tb as New TextBox()
tb .ID = "tbName_" & recIdx.ToString()
tb .Width = Unit.Percentage(100)

' add text box to table cell.

' add table cell to table row.

' add table row to table.

This will add a table row for each record in your data reader with the checkbox and textbox controls.  

When you post the form back to your server, iterate through the table rows like this:

Dim tr as HtmlTableRow
Dim cb as CheckBox
Dim tb as TextBox
Dim rowIdx as Integer

For Each tr in tblTest.Rows
     ' Increment row index (the header row is index 0).

     ' resolve check box and textbox.
     cb = CType(tr.Cells(0).FindControl("cbName_" & rowIdx.ToString()), CheckBox)
     tb = CType(tr.Cells(1).FindControl("tbName_" & rowIdx.ToString()), TextBox)

     ' you can now get the Checked property from cb and the Text property from the textbox and do with it
     ' as needed.

Good luck

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If IsPostBack Then

            Dim i As Integer
            For i = 1 To 5
                If CheckBoxList1.Items.Item(i).Selected Then '.Items(i).Selected Then
                    Dim tb As TextBox
                    tb = FindControl("myText" & i)
                    If i = 1 Then
                        tb.Text = "Test at full load"
                    ElseIf i = 2 Then
                        tb.Text = "Test for leaks"

                    End If
                End If
                Dim i As Integer
                arrPerTest = Split(strPerTestTypes, ",")
                Dim frm As Control
                For i = 1 To UBound(arrPerTest)
                    Dim tb As TextBox
                    tb = New TextBox()
                    tb.ID = "myText" & i
                    frm = Page.FindControl("Form1")
                CheckBoxList1.RepeatColumns = "6"
        End If
    End Sub
what if  i have more than 1 textbox in one row !!!

how can then retrieve text values out of those text boxes !!!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.