Solved

Dynamic CheckBoxes and TextBoxes

Posted on 2004-08-25
3
651 Views
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"
        SqlConnection2.Open()
        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.Items.Add(arrPerTest(i))
        Next
        perCheckBoxList.RepeatColumns = "6"
        SqlConnection2.Close()

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
Sunil



















0
Comment
Question by:svaria
3 Comments
 
LVL 10

Accepted Solution

by:
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">
     <tr>
          <td width="100">Check Box</td>
          <td>Text Box</td>
     </tr>
</table>

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.
recIdx++

' 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.
td1.Controls.Add(cb)

' 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.
td2.Controls.Add(tb)

' add table cell to table row.
tr.Cells.Add(td1)
tr.Cells.Add(td2)

' add table row to table.
tblTest.Rows.Add(tr)
---------------------

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).
     rowIdx++

     ' 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.
Next

Good luck

John
0
 
LVL 35

Expert Comment

by:YZlat
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
            Next
        Else
                Dim i As Integer
                arrPerTest = Split(strPerTestTypes, ",")
                Dim frm As Control
                For i = 1 To UBound(arrPerTest)
                    CheckBoxList1.Items.Add(arrPerTest(i))
                    Dim tb As TextBox
                    tb = New TextBox()
                    tb.ID = "myText" & i
                    frm = Page.FindControl("Form1")
                    frm.Controls.Add(tb)
                Next
                CheckBoxList1.RepeatColumns = "6"
        End If
    End Sub
0
 
LVL 1

Expert Comment

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

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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