crjunk crjunk - 1 year ago 89 Question

Loop Through Controls on Web User Control

I'm having problems looping through controls that are on my user control.

I have tried the following code, but cannot get it to find the checkboxes that are on the user control. (You can see some of my previous attempts that I have commented out.)

'For Each Ctrl As Control In Page.Controls
'For Each Ctrl As Control In Me.Page.Controls
'For Each ctrl As Control In Request.Form
'''Dim frm As Control = Me.FindControl("frmDefault")
'''For Each Ctrl As Control In frm.Controls

Dim Check As CheckBox

For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is CheckBox Then
Check = Ctrl
' Do something here...
End If

There are multiple chekcboxes on the user control. The code shown above is on the code behind page for the user control.

(The user control is being used in conjunction with my CMS, Sitecore. I'm not sure if this has any effect on the problem I am experiencing or not.)

Any suggestions?

Answer Source

I finally figured out what is going on.

I have the checkboxes inside different tables. These tables contains runat="server". This table is inside a Div tag that also contains a runat="server".

My code could never find the checkboxes because of this. I had to add a For Each that loops through the Div tag and find the appropriate table(s). I then had to loop through the tables in order to find the checkboxes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download