Solved

Collection of controls

Posted on 2004-04-21
10
162 Views
Last Modified: 2010-04-07
Using vb.net code behind on asp.net 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
next

Thanks!
0
Comment
Question by:lrr81765
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10880069
Exactly:

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

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

Expert Comment

by:chaniewskim
ID: 10880087
sorry

For Each ctrl As Control In MyControl.Controls
    ' do something
Next
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10880129
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

Next
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
chaniewskim earned 500 total points
ID: 10880194
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)
        Next
    End Sub

And execute it with:

AllControls(Page, 1)

Does it help you?
0
 

Author Comment

by:lrr81765
ID: 10882387
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>
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10882483
You can do

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

B..M
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10882486
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?
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10882504
mmarinov - do you know if it is a 'deep-search', that is it searches also subcontainers?
0
 

Author Comment

by:lrr81765
ID: 10882531
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.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 10882578
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

B..M
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question