If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

I feel like there must be a simpler way to accomplish this. Since ASP.Net doesn't have control arrays like good 'ol VB6 did I'm having difficulty seeing how I could boil this down to a code fragment that might look like the following:

The Controls collection only seems to allow you to reference the controls by index #, no overload for specifying a name (which would be nice). I don't see any methods to allow for this. Anyone know of a way to do this?

This isn't a biggie ... I can manage with the first set of code. I just hate having 5 groups of the same code if I could condense it into a loop!

Thanks,

-Max

If you think it's expensive to hire a professional, wait until you try hiring an amateur! - Red Adair

Re: Referencing Controls on ASP.Net page

cjard,

The idea you posted pointed me in the right direction. It took a little more work to deal with walking the control tree to get this working properly but now my library is proud owner to a new routine which will be very useful.

Code:

/// <summary>
/// Web Version:
///
/// Builds a dictionary containing the references to all controls in the specified controls
/// container so they can be referenced by constructed string names. This provides the ability
/// to build sections of controls and reference them similarly to the way they could have been
/// with control arrays back in the VB6 days.
/// </summary>
/// <param name="container">Object containing the controls to be discovered.</param>
/// <param name="list">Dictionary into which the references should be placed.</param>
///
public void BuildControlList(System.Web.UI.ControlCollection container, Dictionary<string, System.Web.UI.Control> list) {
foreach (System.Web.UI.Control c in container) {
//
// If the current control has a set of controls, recurse and scan the collection.
//
if (c.Controls.Count > 0)
BuildControlList(c.Controls, list);
//
// If this is a leaf node in the controls collection, record its reference in the
// dictionary if its ID is not null.
//
else {
if (c.ID != null)
list[c.ID] = c;
}
}
}

Using that routine in my library now enables me to populate the "control array" I have set up in the following fashion, exactly what I had in mind.