Find first parent control of a specific type using recursion

ASP.NET Add comments

Just like the title says.  There have been some scenarios where I have had to search a controls parents until I find a specific control of type.  A common example would be within a datagrid.  Say you have a button that fires, and you need to to call Update() on a parent updatePanel.  But you don’t want to do something like so:

protected void btn_Click(object sender, ImageClickEventArgs e)
{
    ImageButton button = sender as ImageButton;
    UpdatePanel updatePnl = button.Parent.Parent.Parent as UpdatePanel;
    updatePnl.Update();
}

This will work, but it is definitely not the most elegant way to do something like this, and is dependent on the level of nesting you have within your item template in your datagrid.

So I came up with this solution that is a little nicer, and does not depend on the level of nesting you have in your item templates.  Once again, I’m sure this kind of thing has been done better somewhere, but not that I know of, so this is my solution:

/// <summary>
/// Recursively search a control and its parents, to find the first control
/// of a specific type and return it.
/// </summary>
/// <param name="aParent">Control to recursively search</param>
/// <param name="aType">Type of control to return</param>
/// <returns>First control of type parameter.</returns>
public static Control FindFirstControlOfType(Control aParent, Type aType)
{
    if (aParent.GetType() == aType)
    {
        return aParent;
    } 

    if (aParent.Parent != null)
    {
        return FindFirstControlOfType(aParent.Parent, aType);
    }
    else
    {
        return null;
    }
}

It can then be used in place of the first code block in this post, like so:

protected void btnExpand_Click(object sender, ImageClickEventArgs e)
{
    ImageButton button = sender as ImageButton;
    UpdatePanel updatePnl = 
           FindFirstControlOfType(button, typeof(UpdatePanel)) as UpdatePanel;
    updatePnl.Update();
}

You could easily change this as well to include a control ID parameter.  But for my purposes I didn’t really need to.

About

My name is Brent Krueger and I am an ASP.Net web application developer and designer in Madison, WI. I enjoy doing web design and development for charitable organizations, as well as businesses. I also like to post any solutions I may find to irritating development problems that I may run into on a daily basis.

3 Responses to “Find first parent control of a specific type using recursion”

  1. Steve Says:

    Or with generics and extension methods…

    public static T FindParentOfType(this Control control) where T : Control
    {
    if (control == null)
    return null;

    if (control.Parent == null)
    return null;

    if (control.Parent is T)
    {
    return (T)control.Parent;
    }
    return FindParentOfType(control.Parent);
    }

    ====

    var dockPanel = this.FindParentOfType();
    if (dockPanel != null)
    {…}

  2. Brent Says:

    Thank you Steve, like I said, mine was definitely not the best way to go. Your approach is much more elegant, and great use of generics. I will give it a shot.

  3. Brent Says:

    This should do it, thanks Steve.

    public static T FindParentOfType(Control aParent) where T : Control
    {
    if (aParent is T)
    {
    return (T)aParent;
    }

    if (aParent.Parent != null)
    {
    return FindParentOfType(aParent.Parent);
    }

    return null;
    }

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in