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.

There are 3 comments .

Steve —

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

Reply »
    Brent

    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.

    Reply »
    Brent

    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;
    }

    Reply »

Share Your Thoughts!

Testimonials

What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!

Dan

I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at brent@kruegerwebdesign.com or drop me a line using the button below:

Copyright © 2017 Krueger Web Design - All rights reserved
Web Design in Madison, WI