Link to home
Create AccountLog in
Avatar of G0ggy
G0ggyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Loop through dynamic controls contained by a master page

Hi,

I'm trying to look through some controls which are dynamically created on my webpage, the creating code is this:

For Each row As DataRow In ds.Tables("Questions").Rows
                Dim lbl As Label = New Label()
                lbl.ID = i
                lbl.Style("margin-top") = i
                lbl.Style("margin-left") = "10px"
                lbl.Style("width") = "200px"
                lbl.SkinID = "question"
                lbl.Style("padding-top") = "20px"
                lbl.Style("padding-bottom") = "10px"

                Dim question As String = row("questionText").ToString
                lbl.Text = Convert.ToString(x) & ": " & question
                pnlAssessment.Controls.Add(lbl)

                If row("questiontype") = 37 Then
                    Dim img As Image = New Image()
                    Dim test As String = Server.MapPath("~/secure/images/aqimages/" & row("questionImageFilename").ToString)
                    img.ImageUrl = Server.MapPath("~/secure/images/aqimages/" & row("questionImageFilename").ToString)
                    img.Width = 700
                    img.Height = 300
                    pnlAssessment.Controls.Add(img)
                End If

                Dim childRows As DataRow() = row.GetChildRows(relation)

                For Each childRow As DataRow In childRows
                    Dim rbl As CheckBoxList = New CheckBoxList()

                    rbl.ID = "qid" & row("questionid").ToString
                    rbl.SkinID = "checkboxlist"
                    Dim tPos As Integer = 60 * (i + 1)
                    rbl.Style("margin-top") = i + 20
                    rbl.Style("margin-left") = "50px"
                    rbl.Items.Add(childRow("answerText".ToString))
                    pnlAssessment.Controls.Add(rbl)
                Next
                x = x + 1
            Next

However, if I look through the controls:

For Each ctrl As Control In Form.Controls
            System.Diagnostics.Debug.WriteLine("Control " & ctrl.UniqueID & vbCrLf)
        Next

All I get back is this:

Control ctl00$ctl07

Control ctl00$lblWelcomeMessage

Control ctl00$ctl08

Control ctl00$imgJCBLogo

Control ctl00$ctl09

Control ctl00$mainMenu

Control ctl00$ctl10

Control ctl00$smpMain

Control ctl00$ctl11

Control ctl00$cphNew2ColMain

Control ctl00$ctl12

Control ctl00$cphNew2ColRight

Control ctl00$ctl13

Control ctl00$lblVersionInfo

Control ctl00$ctl14

Control ctl00$lblCopyright

Control ctl00$ctl15

Control ctl00$hplPrivacy

Control ctl00$ctl16

Control ctl00$hplDPA

Control ctl00$ctl17

Control ctl00$lnbRemoveLoginCookies

Control ctl00$ctl18

Control ctl00$cookies

Control ctl00$ctl19

It looks like it is just containing the dynamic control within the master page and not parsing them. How can I get the ID's of these dynamically created controls?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

It doesn't look like you are recursively looking at child controls for those top-level controls in the page control tree.

Bob
ASKER CERTIFIED SOLUTION
Avatar of G0ggy
G0ggy
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The main question right now is, "Are those controls rendered on the output page (View Source)?"

Bob
Avatar of G0ggy

ASKER

Yes they are, snippet attached.
<div id="ctl00_cphNew2ColMain_pnlAssessment">
	<span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">1: State the units of measurement for electrical potential?</span><table id="ctl00_cphNew2ColMain_qid1" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid1_0" type="checkbox" name="ctl00$cphNew2ColMain$qid1$0" /><label for="ctl00_cphNew2ColMain_qid1_0">Joules</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid1_1" type="checkbox" name="ctl00$cphNew2ColMain$qid1$1" /><label for="ctl00_cphNew2ColMain_qid1_1">Watts</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid1_2" type="checkbox" name="ctl00$cphNew2ColMain$qid1$2" /><label for="ctl00_cphNew2ColMain_qid1_2">Ohms</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid1_3" type="checkbox" name="ctl00$cphNew2ColMain$qid1$3" /><label for="ctl00_cphNew2ColMain_qid1_3">Amps</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid1_4" type="checkbox" name="ctl00$cphNew2ColMain$qid1$4" /><label for="ctl00_cphNew2ColMain_qid1_4">Volts</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">2: What is the correct equation for Ohm's law?</span><table id="ctl00_cphNew2ColMain_qid2" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid2_0" type="checkbox" name="ctl00$cphNew2ColMain$qid2$0" /><label for="ctl00_cphNew2ColMain_qid2_0">R = V + I</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid2_1" type="checkbox" name="ctl00$cphNew2ColMain$qid2$1" /><label for="ctl00_cphNew2ColMain_qid2_1">R = V x I</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid2_2" type="checkbox" name="ctl00$cphNew2ColMain$qid2$2" /><label for="ctl00_cphNew2ColMain_qid2_2">I = V - R</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid2_3" type="checkbox" name="ctl00$cphNew2ColMain$qid2$3" /><label for="ctl00_cphNew2ColMain_qid2_3">I = V x R</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid2_4" type="checkbox" name="ctl00$cphNew2ColMain$qid2$4" /><label for="ctl00_cphNew2ColMain_qid2_4">V = I - R</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid2_5" type="checkbox" name="ctl00$cphNew2ColMain$qid2$5" /><label for="ctl00_cphNew2ColMain_qid2_5">V = I + R</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid2_6" type="checkbox" name="ctl00$cphNew2ColMain$qid2$6" /><label for="ctl00_cphNew2ColMain_qid2_6">V = I x R</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">3: When measuring current, how should the multimeter be connected into the circuit?</span><table id="ctl00_cphNew2ColMain_qid3" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid3_0" type="checkbox" name="ctl00$cphNew2ColMain$qid3$0" /><label for="ctl00_cphNew2ColMain_qid3_0">Parallel</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid3_1" type="checkbox" name="ctl00$cphNew2ColMain$qid3$1" /><label for="ctl00_cphNew2ColMain_qid3_1">Series</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">4: Which component converts the alternating current generated by an alternator into direct current for use on the vehicle?</span><table id="ctl00_cphNew2ColMain_qid4" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid4_0" type="checkbox" name="ctl00$cphNew2ColMain$qid4$0" /><label for="ctl00_cphNew2ColMain_qid4_0">Pole shoes</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid4_1" type="checkbox" name="ctl00$cphNew2ColMain$qid4$1" /><label for="ctl00_cphNew2ColMain_qid4_1">Field coil</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid4_2" type="checkbox" name="ctl00$cphNew2ColMain$qid4$2" /><label for="ctl00_cphNew2ColMain_qid4_2">Commutator</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid4_3" type="checkbox" name="ctl00$cphNew2ColMain$qid4$3" /><label for="ctl00_cphNew2ColMain_qid4_3">Stator</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid4_4" type="checkbox" name="ctl00$cphNew2ColMain$qid4$4" /><label for="ctl00_cphNew2ColMain_qid4_4">Rectifier</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid4_5" type="checkbox" name="ctl00$cphNew2ColMain$qid4$5" /><label for="ctl00_cphNew2ColMain_qid4_5">Regulator</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">5: State the unit of measurement for electrical current</span><table id="ctl00_cphNew2ColMain_qid5" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid5_0" type="checkbox" name="ctl00$cphNew2ColMain$qid5$0" /><label for="ctl00_cphNew2ColMain_qid5_0">Joules</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid5_1" type="checkbox" name="ctl00$cphNew2ColMain$qid5$1" /><label for="ctl00_cphNew2ColMain_qid5_1">Watts</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid5_2" type="checkbox" name="ctl00$cphNew2ColMain$qid5$2" /><label for="ctl00_cphNew2ColMain_qid5_2">Ohms</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid5_3" type="checkbox" name="ctl00$cphNew2ColMain$qid5$3" /><label for="ctl00_cphNew2ColMain_qid5_3">Amps</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid5_4" type="checkbox" name="ctl00$cphNew2ColMain$qid5$4" /><label for="ctl00_cphNew2ColMain_qid5_4">Volts</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">6: JCB user wire numbers as opposed to colours. What wires are designated by numbers in the range 400 to 599?</span><table id="ctl00_cphNew2ColMain_qid6" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid6_0" type="checkbox" name="ctl00$cphNew2ColMain$qid6$0" /><label for="ctl00_cphNew2ColMain_qid6_0">Battery feeds (fused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid6_1" type="checkbox" name="ctl00$cphNew2ColMain$qid6$1" /><label for="ctl00_cphNew2ColMain_qid6_1">Battery feeds (unfused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid6_2" type="checkbox" name="ctl00$cphNew2ColMain$qid6$2" /><label for="ctl00_cphNew2ColMain_qid6_2">Ignition feeds (fused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid6_3" type="checkbox" name="ctl00$cphNew2ColMain$qid6$3" /><label for="ctl00_cphNew2ColMain_qid6_3">Ignition feeds (unfused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid6_4" type="checkbox" name="ctl00$cphNew2ColMain$qid6$4" /><label for="ctl00_cphNew2ColMain_qid6_4">Instruments</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid6_5" type="checkbox" name="ctl00$cphNew2ColMain$qid6$5" /><label for="ctl00_cphNew2ColMain_qid6_5">Switched supplies</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">7: The main battery cable from the battery to the starter motor is, commonly, in excess of 10mm diameter wire. Why is it so large?</span><table id="ctl00_cphNew2ColMain_qid7" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid7_0" type="checkbox" name="ctl00$cphNew2ColMain$qid7$0" /><label for="ctl00_cphNew2ColMain_qid7_0">Large diameter wires are low resistance, so pass low current to the starter</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid7_1" type="checkbox" name="ctl00$cphNew2ColMain$qid7$1" /><label for="ctl00_cphNew2ColMain_qid7_1">Large diameter wires are high resistance, so pass high current to the starter</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid7_2" type="checkbox" name="ctl00$cphNew2ColMain$qid7$2" /><label for="ctl00_cphNew2ColMain_qid7_2">Large diameter wires are low resistance, so pass high current to the starter</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid7_3" type="checkbox" name="ctl00$cphNew2ColMain$qid7$3" /><label for="ctl00_cphNew2ColMain_qid7_3">Large diameter wires are high resistance, so pass low current to the starter</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">8: In the circuit shown, a solenoid is connected in series with a 12v battery and switch. When the switch is closed, what voltage would you expect to measure from the battery side of the solenoid to earth?</span><img src="C:\Inetpub\wwwroot\lms\secure\images\aqimages\basic-electrics-elec-circuit.jpg" style="height:300px;width:700px;border-width:0px;" /><table id="ctl00_cphNew2ColMain_qid10" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid10_0" type="checkbox" name="ctl00$cphNew2ColMain$qid10$0" /><label for="ctl00_cphNew2ColMain_qid10_0">6v</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid10_1" type="checkbox" name="ctl00$cphNew2ColMain$qid10$1" /><label for="ctl00_cphNew2ColMain_qid10_1">12v</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid10_2" type="checkbox" name="ctl00$cphNew2ColMain$qid10$2" /><label for="ctl00_cphNew2ColMain_qid10_2">0v</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">9: State the unit of measurement for electrical resistance?</span><table id="ctl00_cphNew2ColMain_qid11" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid11_0" type="checkbox" name="ctl00$cphNew2ColMain$qid11$0" /><label for="ctl00_cphNew2ColMain_qid11_0">Joules</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid11_1" type="checkbox" name="ctl00$cphNew2ColMain$qid11$1" /><label for="ctl00_cphNew2ColMain_qid11_1">Watts</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid11_2" type="checkbox" name="ctl00$cphNew2ColMain$qid11$2" /><label for="ctl00_cphNew2ColMain_qid11_2">Ohms</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid11_3" type="checkbox" name="ctl00$cphNew2ColMain$qid11$3" /><label for="ctl00_cphNew2ColMain_qid11_3">Amps</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid11_4" type="checkbox" name="ctl00$cphNew2ColMain$qid11$4" /><label for="ctl00_cphNew2ColMain_qid11_4">Volts</label></td>
		</tr>
	</table><span id="ctl00_cphNew2ColMain_10" style="color:#464646;font-family:Verdana;font-size:12px;font-weight:bold;margin-top:10;margin-left:10px;width:200px;padding-top:20px;padding-bottom:10px;">10: JCB use wire numbers as opposed to colours. What wires are designated by numbers in the range 600 to 799?</span><table id="ctl00_cphNew2ColMain_qid12" border="0" style="color:#464646;font-family:Verdana;font-size:12px;margin-top:30;margin-left:50px;">
		<tr>
			<td><input id="ctl00_cphNew2ColMain_qid12_0" type="checkbox" name="ctl00$cphNew2ColMain$qid12$0" /><label for="ctl00_cphNew2ColMain_qid12_0">Battery feeds (fused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid12_1" type="checkbox" name="ctl00$cphNew2ColMain$qid12$1" /><label for="ctl00_cphNew2ColMain_qid12_1">Battery feeds (unfused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid12_2" type="checkbox" name="ctl00$cphNew2ColMain$qid12$2" /><label for="ctl00_cphNew2ColMain_qid12_2">Ignition feeds (fused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid12_3" type="checkbox" name="ctl00$cphNew2ColMain$qid12$3" /><label for="ctl00_cphNew2ColMain_qid12_3">Ignition feeds (unfused)</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid12_4" type="checkbox" name="ctl00$cphNew2ColMain$qid12$4" /><label for="ctl00_cphNew2ColMain_qid12_4">Earths</label></td>
		</tr><tr>
			<td><input id="ctl00_cphNew2ColMain_qid12_5" type="checkbox" name="ctl00$cphNew2ColMain$qid12$5" /><label for="ctl00_cphNew2ColMain_qid12_5">Instruments</label></td>
		</tr>
	</table>
</div>

Open in new window

I see that the controls are part of an HTML table.  You would need to go through the rows and the cells in the table, and get a reference to the controls from the cell.

Bob
Avatar of G0ggy

ASKER

Isn't the HTML for the table rendered by the check box list control? The code is as in the previous post.
Try this:


Imports System.Collections.Generic
 
...
 
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim list As New List(Of Control)
    Me.GetControlsRecursively(list, Me.Controls)
  End Sub
 
  Private Sub GetControlsRecursively(ByVal list As List(Of Control), ByVal controlCollection As ControlCollection)
 
    For Each ctl As Control In controlCollection
      list.Add(ctl)
 
      If ctl.HasControls() Then
        GetControlsRecursively(list, ctl.Controls)
      End If
    Next ctl
 
  End Sub

Open in new window

Avatar of G0ggy

ASKER

Afraid it didn't work, just produced the same output as before.
List contains:48
Control: 
Control: 
Control: 
Control: 
Control: 
Control: 
Control: 
Control: 
Control: 
Control: 
Control: 
Control: form1
Control: 
Control: lblWelcomeMessage
Control: 
Control: imgJCBLogo
Control: 
Control: mainMenu
Control: 
Control: smpMain
Control: 
Control: cphNew2ColMain
Control: 
Control: pnlAssessment
Control: 
Control: btnSubmitAnswers
Control: 
Control: 
Control: cphNew2ColRight
Control: 
Control: lblAssessmentSubTitle
Control: 
Control: lblAssessmentWarning
Control: 
Control: 
Control: lblVersionInfo
Control: 
Control: lblCopyright
Control: 
Control: hplPrivacy
Control: 
Control: hplDPA
Control: 
Control: lnbRemoveLoginCookies
Control: 
Control: cookies
Control: 
Control: 

Open in new window

Avatar of G0ggy

ASKER

Here's the code for reference.
 Shared Sub Print(ByVal s As Control)
        System.Diagnostics.Debug.WriteLine("Control: " & s.ID)
    End Sub
 
    Protected Sub btnSubmitAnswers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitAnswers.Click
                Dim list As New List(Of Control)
 
        Me.getControlsRecursively(list, Me.Controls)
 
        System.Diagnostics.Debug.WriteLine("List contains:" & list.Count)
 
        list.ForEach(AddressOf Print)

Open in new window

1) The page posts back when you click the <Submit> button.

2) When the page posts back, are you re-adding the dynamic controls?

Bob
Avatar of G0ggy

ASKER

It does at the moment, do you think that is the problem? I'm just trying to get the values for now before trying to do anything else...
If you don't see a control on a page, then my first inclination is that it is not part of the control tree at the point that you are checking.

For those controls that don't have an ID, I would check their type:

System.Diagnostics.Debug.WriteLine("Control: " & s.GetType().Name)


Bob
Avatar of G0ggy

ASKER

Well I got Microsoft involved in the end. The couldn't get my code to work, but they imported it into a new project which then it worked, i.e. you could retrieve that values of the ID's of the checkboxes. I am now clueless as to why it works in their 'new' project (which is built of the pages in my project) works, and mine doesn't.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of G0ggy

ASKER

Another update, I deleted my page then re-created it, now it works...

Bah :( 2 weeks wasted.