Collection of controls

Using code behind on ASCX control, is there a way to enumerate all controls on the control?

I need to do something like
for each _control on _usercontrol
   'do something

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


For Each ctrl As WebControl In MyControl.Controls
    ' do something

but it would enumerate only controls directly inside MyControl.
To enumerate all in-depth you have to use recurssion.

For Each ctrl As Control In MyControl.Controls
    ' do something
You can also use TypeOf and CType to determine what kind of control you're at, and to use it in appropriate way:

For Each ctrl As Control In MyControl.Controls

If TypeOf ctrl Is TextBox Then
     CType(ctrl, TextBox).Text = "test"
End If

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

OK, it's like a TV series... ;)

To traverse all controls on page and print an indented controls tree, use this recursive Sub:

    Private Sub AllControls(ByVal startControl As Control, ByVal level As Integer)
        For Each ctrl As Control In startControl.Controls
            System.Diagnostics.Debug.WriteLine(Space(level) + ctrl.ID)
            AllControls(ctrl, level + 1)
    End Sub

And execute it with:

AllControls(Page, 1)

Does it help you?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lrr81765Author Commented:
Ok, I can see that but I still having a couple of issues.

I am assuming that "MyControl" should be replaced with the name of the control (ICE in my case) , or is this one of those special things like Me and MyBase ?

What I am trying to do is store the info from a form (many controls) into a database, and set them from the database.

I have this:
        Public Shared Sub SetControl(ByVal controlname As String, ByVal value As String)

        End Sub
and I know that I will have a big select statement to determine the types, but is there any way to get the control based on the name ? (the controlname will equal the control on the form)

Thanks so much>
You can do

you can use CType(Me.FindControl("youttextboxcontrolid"), TextBox )

Yes, replace that name - it is only an example :)

The rest - I don't understand. Do you want to find a control given you know it's name?
mmarinov - do you know if it is a 'deep-search', that is it searches also subcontainers?
lrr81765Author Commented:
Thanks I'll try that now. Yes, I'll know the control name.
I have a table that stores name/value, where the name =controlname and value=text,selectedvalue, etc.
I don't think it is a "deep search". I have a couple of panels that hold controls, but that is it.
chaniewskim, i don't know if it is a deep-search
but if it is - it won't be a problem :)
you can do recurse function and make your sql statement so easy - all depends of your design

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.