ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

FindParent

A simple type safe method to find a parent control

Source

public static T FindParent<T>(this Control target) where T : Control
{
    if (target.Parent == null)
    {
        return null;
    }

    var parent = target.Parent as T;
    if (parent != null)
    {
        return parent;
    }

    return target.Parent.FindParent<T>();
}

Example

someControl.FindParent<RepeaterItem>().UniqueID;

Author: Rob White

Submitted on: 13 dec. 2010

Language: C#

Type: System.Web.UI.Control

Views: 6690