santosh singh santosh singh - 1 year ago 100
ASP.NET (C#) Question

Better way to find control in ASP.NET

I have a complex form,having even 50 to 60 fields in one form like there is

, inside MultiView I have a
, and inside GridView I have several

Currently I am using chaining of the
method and retrieving the child ID.

Now, my question is that is there any other way/solution to find the nested control in ASP.NET.

Answer Source

If you're looking for a specific type of control you could use a recursive loop like this one -

Here's an example I made that returns all controls of the given type

/// <summary>
/// Finds all controls of type T stores them in FoundControls
/// </summary>
/// <typeparam name="T"></typeparam>
private class ControlFinder<T> where T : Control 
    private readonly List<T> _foundControls = new List<T>();
    public IEnumerable<T> FoundControls
        get { return _foundControls; }

    public void FindChildControlsRecursive(Control control)
        foreach (Control childControl in control.Controls)
            if (childControl.GetType() == typeof(T))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download