easier way to collect controls on a page?

  • Thread starter Thread starter David
  • Start date Start date
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.
 
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
 
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}
 
Back
Top