Solved

Collection of controls

Posted on 2004-04-21
10
158 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now