Solved

Dynamic CheckBoxes and TextBoxes

Posted on 2004-08-25
3
649 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now