Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Recursive itteration through controls in a table.

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
  • 4
  • 4
1 Solution
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.
EsulinAuthor Commented:
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

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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

EsulinAuthor Commented:
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.
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
EsulinAuthor Commented:
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 checkbox.id 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?

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 asp.net.  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.

EsulinAuthor Commented:
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

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!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now