?
Solved

Collection of controls

Posted on 2004-04-21
10
Medium Priority
?
165 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
Technology Partners: 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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