control collection

egdotnet

Regular
Joined
May 16, 2004
Messages
50
when I use a loop:

Code:
For each mycontrol in panel1.controls
if typeof(mycontrol) is textbox then
     dim mytextbox as textbox = mycontrol
     label1.text = "dog"
elseif typeof(mycontrol) is label) then
     dim mylabel as label = mycontrol
     label1.text = "cat"
End If
Next


It only works for controls added in design view rather than those previously added programatically. Why is that & how do I change it?

Thanks
 
Last edited:
previously added programatically

Where did you add those controls to? Your code only loops through the controls that panel1 contains. So if a control is added to the page/form's control collection, you won't be able to find it. Also if a control inside panel1's control collection is a container, your loop does not go through the control collection of that container as well. For example, lets say if panel1 contains a PlaceHolder control called placeholder1, and placeholder1 contains textbox1. Your loop will find placeholder1, but NOT textbox1.

Not sure if this helps.
 
bob01900 said:
Where did you add those controls to? Your code only loops through the controls that panel1 contains. So if a control is added to the page/form's control collection, you won't be able to find it. Also if a control inside panel1's control collection is a container, your loop does not go through the control collection of that container as well. For example, lets say if panel1 contains a PlaceHolder control called placeholder1, and placeholder1 contains textbox1. Your loop will find placeholder1, but NOT textbox1.

Not sure if this helps.

thanks -- they were in a panel inside, but when i took it out, it didn't make a difference.
 
Try this then, add the following just before the loop:

Code:
Dim ctrl As Control
' Change textbox1 to the ID of the control that you are looking for
ctrl = Me.FindControl("textbox1")
' Change to debug or trace if you are using them
If Not ctrl Is Nothing Then
    Response.Write(ctrl.Parent.ID.ToString())
Else
    Response.Write("No controls found")
End If
This should show you where the control is, or if they are created at all. Hope this helps.
 
Where is this code in relation to the code that dynamically adds controls to the page?
So, when the page loads, is the control-creation code executed before this code?
 
Back
Top