Recursive itteration through controls in a table.

Posted on 2005-04-14
Last Modified: 2011-04-14
Ugh.. This is driving me crazy...

I use the following code to populate a asp:table (tblDsCategories) with cells containing checkboxes which reprsent records from a table in my sql database.
Ignore the nested loops as they are only there to populate the table using three columns.
    Private Sub sPopulateCategoryTable(Optional ByVal ContactId As Integer = 0)
        Dim tTable As TableRowCollection

        Dim oConn As New cSql
        Dim sSQl As String
        If ContactId > 0 Then

                sSQl = "Select tCategories.tCategory_id, tCategories.tCategory_Name From tCategories"
                Dim dsCategories As DataSet = oConn.GetDataSet(sSQl, "tCategories")
                Dim tblDsCategories As DataTable = dsCategories.Tables(0)

                Dim i, j As Integer
                Dim uLimit As Integer = tblDsCategories.Rows.Count - 1
                While i <= uLimit
                    Dim tRow As New TableRow
                    For j = 0 To 2

                        Dim tCell As New TableCell
                        Dim cCheckBox As New CheckBox
                        Dim lLabel As New Label

                        cCheckBox.ID = "chk_Cat_" & tblDsCategories.Rows(i).Item("tCategory_id")
                        lLabel.Text = tblDsCategories.Rows(i).Item("tCategory_Name")


                        i += 1
                        If i >= uLimit Then
                            Exit For
                        End If
                End While
    End Sub

Now I'm trying to itterate through all the checkboxes and to put them in an ArrayList. The following is the procedure call and the sub:

getCheckBoxes("chk_cat", tblCategory, arrCheckBoxes)   'arrCheckboxes is defined as an arrayList.

    Private Sub getCheckBoxes(ByVal LookFor As String, ByRef LookIn As Object, ByRef arrCheckBoxes As ArrayList)
        Dim ctl As Object
        Dim controlCount As String = LookIn.Controls.Count   'This currently returns 0, and the for loop does not execute.
        Dim chk As CheckBox
        For Each ctl In LookIn.Controls
            If Left(ctl.ID, 7) = LookFor Then
                arrCheckBoxes.Add(DirectCast(ctl, CheckBox))
                If ctl.Controls.Count > 0 Then
                    getCheckBoxes(LookFor, ctl, arrCheckBoxes)
                End If
            End If
    End Sub

So, for some reason, even though this code is a copy/paste of another snippet of code which works fine, the loop does not execute because there are no controls in LookIn.Controls

Help. :)

- Esulin
Question by:Esulin
    LVL 33

    Expert Comment

    Where is "LookIn" declared?  Are you trying to iterate through the controls after a postback per chance?  This is just going to get yucky from here if this is the case.

    Author Comment

    LookIn is passed "tblCategory", which is the asp:table that I populate with rows, cells, and finally checkboxes in the first snippet of code.

    Funnily enough I got this working with bunch of textBox and DropDown controls in another table on the same page, under pretty much the same circumstances. I think I'm just missing something small somewhere.

    - Esulin

    LVL 33

    Expert Comment

    You may have some conversion issues, as I see you are not properly casting some assignments, I'd try setting "Option Strict On" at the top of your source page, then fix the errors that you are notified about.

    For example:

    Dim controlCount As String = LookIn.Controls.Count   'This currently returns 0, and the for loop does not execute.
    should be
    Dim controlCount As Integer = LookIn.Controls.Count   'This currently returns 0, and the for loop does not execute.

    Author Comment

    lol... That's only there so that I can see how many controls there are when I trace the program (should have deleted it). And controlCount is not used anywhere anyway.
    LVL 33

    Expert Comment

    That is just one example, the easiest to demonstrate.  There is another conversion issue you may not see though.  You are passing LookIn as an Object, but using it like a control.  The Object passed doesn't necessarily have a "Controls" collection.  Set Option Strict On, you should see what I mean.

    anyway, this probably isn't your problem, just a good coding practice.

    Tell me more about this LookIn control, Is this code running on a postback, and hasn't been populated this time around?  More code would be helpful

    Author Comment

    Hmmm... More code would be difficult... The page is quite complex and large.

    Basically at the moment I display a set of Contact data from a table for the purposes of editing in a series of textboxes, below which I have the above code which generates a series of chkboxes which reprsent links between the the Contact and the ContactCategory which uses a linking table.. a many-to-many relationship.

    What working on now is updating the database with the data from the textboxes and checkboxes. I already have the textboxes covered, and that works fine.

    Basically, the user clicks a button which first executes the Update based on the textboxes followed by another update using the checkboxes. I need to itterate through all the checkboxes, identify the chekced ones and extract the integer value which is stored in the in the form of chk_cat_1, chk_cat_2 etc.

    So, yes, the page is on a postback. The asp table with the checkboxes is nested in another asp table which holds the textboxes, which is nested in an HTML Table used for layout. As you can see from the code above, the checkboxes are dynamically.

    The recursive procedure that I created uses LookIn to which I'm currently passing the ID of the table that contains the checkboxes (tblCategory).

    Need to know anything else?

    LVL 33

    Accepted Solution

    Since the controls are dynamically created, you need to recreate these controls on a postback, or else they are gone for good.  I'd imagine if the LookIn control has no controls of itself after you postback, then the recreation process is not happening correctly.

    If your up for a challenge, I think you can better your current strategy if you redo this dynamic table as a Repeater control.  This gets rid of the dynamic control problem, and is much more "elegant".  I speak with experience, I just redesigned a huge page I did when I first started  I had the one big table, and inserted rows/cells/controls into it like crazy, which I would have to pull on a postback.    After it was complete I thought it was the greatest page I've ever done, but the more I had to add/change things, the more I started to see how completely off I was in my original design.  I redesigned the page with a Repeater and Usercontrols and now it is much nicer, and I have 0 dynamic controls I have to worry about.  Just something to think about.


    Author Comment

    Hmmm.. I didn't realise that the controls no longer exist after a postback.

    The idea of using the Repeater control sounds pretty good. I'll give that a go.

    Thanks Michael... Again. :)


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    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,…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 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

    15 Experts available now in Live!

    Get 1:1 Help Now