Solved

Collection of controls

Posted on 2004-04-21
10
161 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 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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