Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

when I use a loop:

 

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

Edited by egdotnet
Posted
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.

Posted
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.

Posted

Try this then, add the following just before the loop:

 

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.

  • *Experts*
Posted

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?

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...