Find first parent control of a specific type using recursion

Find first parent control of a specific type using recursion

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.

By | 2011-01-05T22:53:16+00:00 January 5th, 2011|Categories: ASP.NET|Tags: , |3 Comments

About the Author:

3 Comments

  1. Steve December 22, 2011 at 12:35 am - Reply

    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)
    {…}

    • Brent January 30, 2012 at 10:50 pm - Reply

      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.

    • Brent January 31, 2012 at 5:53 pm - Reply

      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 Comment