Solved

Collection of controls

Posted on 2004-04-21
10
159 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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