FindImmediateParentOfType<T>

public static T FindImmediateParentOfType<T>(this Control control) where T : Control
    {
        T retVal = default(T);
        Control parentCtl = control.Parent;
        while (parentCtl != null)
        {
            if (parentCtl is T)
            {
                retVal = (T)parentCtl;
                break;
            }
            else
            {
                parentCtl = parentCtl.Parent;
            }
        }
        return retVal;
 
    }
Example:
TextBox tb = getTextBox();

DataList parentDataList = tb.FindImmediateParentOfType<DataList>();

Description

An extension method to find the parent control of a specific type in asp.net

Details

Double click on the code to select all.

 

;