Control Array

  • Thread starter Thread starter Jim McGivney
  • Start date Start date
J

Jim McGivney

I am working on an ASP.net project with code behind in C#.
I must manipulate the contents of many label controls.
In visual basic (VB6) I could place all the controls into a control array
and then refer to an individual control it's index number.
I would like to do the same in C#.
Can someone suggest a method I should use ?
Thanks,
Jim
 
The controls are already in a collection that you can iterate through.
The property is called Controls. The only thing is that it's *all*
the controls, so you have to check the type of each control instance in
the collection in order to find just the labels. Also you may have to
recurse to deal with controls that contain other controls (for example
in your scenario, now or in the future, some labels may be within panels).

This is a pain rather than just defining a control array, but it can be
done with utility code that you write once and forget. For example you
could get a simple ArrayList of label references back from a routine
that iterates the control collection, along the lines:

ArrayList myLabels =
Util.GetControlList(Controls,typeof(System.Web.Controls.Label);

//or:

ArrayList myLabels = Util.GetControlsByNamePrefix(Controls,"lbl");

For some ideas, Google on something like:

..NET Controls iterate

Best,

--Bob
 

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

Back
Top