easier way to collect controls on a page?

D

David

My goal is to collect all the textboxes on a given page in code (or all
checkboxes).

The only way that I know how is traversing through the hierarchy and collect
them

Page
|__Controls
|____HtmlForm
|__text boxes found here

The trouble is controls are embedded in others such as user controls, panel,
table, datagrid, etc, in which case, I'd have to look in there to find them
and there is no way I can write code to cover every possible hierarchy and
chase them down. Is there a cleaner way of doing this? I suppose I can
write a recursive function to do this.

If on a given web form, I want to collect all textboxes. As simple as that,
but hierarchies can be pretty complex.
 
J

Jos

David said:
My goal is to collect all the textboxes on a given page in code (or all
checkboxes).

The only way that I know how is traversing through the hierarchy and collect
them

Page
|__Controls
|____HtmlForm
|__text boxes found here

The trouble is controls are embedded in others such as user controls, panel,
table, datagrid, etc, in which case, I'd have to look in there to find them
and there is no way I can write code to cover every possible hierarchy and
chase them down. Is there a cleaner way of doing this? I suppose I can
write a recursive function to do this.

If on a given web form, I want to collect all textboxes. As simple as that,
but hierarchies can be pretty complex.

A recursive function is the way to go (VB.NET):

Sub DoControls(ctl As Control)
If(TypeOf ctl Is TextBox) Then
' do your stuff here
End If
Dim myControl As Control
For Each myControl In ctl.Controls
DoControls(myControl)
Next
End Sub
 
J

John Saunders

David said:
My goal is to collect all the textboxes on a given page in code (or all
checkboxes).

The only way that I know how is traversing through the hierarchy and collect
them

Page
|__Controls
|____HtmlForm
|__text boxes found here

The trouble is controls are embedded in others such as user controls, panel,
table, datagrid, etc, in which case, I'd have to look in there to find them
and there is no way I can write code to cover every possible hierarchy and
chase them down. Is there a cleaner way of doing this? I suppose I can
write a recursive function to do this.

If on a given web form, I want to collect all textboxes. As simple as that,
but hierarchies can be pretty complex.

What happens in the future when you change one of the TextBox controls into
a RichTextBox control or some other control?

One awkward way to do this would be to store references to all of the
controls in an array at runtime:

Dim ca As New Control(){txtBox1, txtBox2, rtxBox3}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top