Dynamic CheckBoxes and TextBoxes

Posted on 2004-08-25
Last Modified: 2012-08-13
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

Question by:svaria
LVL 10

Accepted Solution

jnhorst earned 125 total points
ID: 11896390
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

LVL 35

Expert Comment

ID: 11896560
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

Expert Comment

ID: 12272747
what if  i have more than 1 textbox in one row !!!

how can then retrieve text values out of those text boxes !!!!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
daypilot scheduler 3 35
Convert Select to DropDownListFor MVC 5 2 29
Function after success of Jquery/AJAX action 9 27
Add or delete table rows 10 53
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question