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.