Esulin
asked on
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(Opt ional ByVal ContactId As Integer = 0)
Dim tTable As TableRowCollection
Dim oConn As New cSql
oConn.Open()
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).It em("tCateg ory_id")
lLabel.Text = tblDsCategories.Rows(i).It em("tCateg ory_Name")
tCell.Controls.Add(cCheckB ox)
tCell.Controls.Add(lLabel)
tRow.Cells.Add(tCell)
i += 1
If i >= uLimit Then
Exit For
End If
Next
tblCategory.Rows.Add(tRow)
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(DirectCa st(ctl, CheckBox))
Else
If ctl.Controls.Count > 0 Then
getCheckBoxes(LookFor, ctl, arrCheckBoxes)
End If
End If
Next
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
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(Opt
Dim tTable As TableRowCollection
Dim oConn As New cSql
oConn.Open()
Dim sSQl As String
If ContactId > 0 Then
sSQl = "Select tCategories.tCategory_id, tCategories.tCategory_Name
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
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).It
lLabel.Text = tblDsCategories.Rows(i).It
tCell.Controls.Add(cCheckB
tCell.Controls.Add(lLabel)
tRow.Cells.Add(tCell)
i += 1
If i >= uLimit Then
Exit For
End If
Next
tblCategory.Rows.Add(tRow)
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(DirectCa
Else
If ctl.Controls.Count > 0 Then
getCheckBoxes(LookFor, ctl, arrCheckBoxes)
End If
End If
Next
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
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.
ASKER
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
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.
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.
ASKER
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
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
ASKER
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?
-Esulin
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?
-Esulin
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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. :)
Regards,
-Esulin
The idea of using the Repeater control sounds pretty good. I'll give that a go.
Thanks Michael... Again. :)
Regards,
-Esulin