Solved

Dynamic CheckBoxes and TextBoxes

Posted on 2004-08-25
3
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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