How to check if one control is the son of another? "Control.Ischildof"

Asked

Viewed 111 times

1

I have 3 panels:

<asp:Panel ID="ParentPanel" runat="server">
    <asp:Panel ID="AnnoyingPanel" runat="server">
        <asp:Panel ID="P" runat="server">
        </asp:Panel>
    </asp:Panel>
</asp:Panel>

How can I verify that P is descended from ParentPanel?

  • Dude, are you translating the QA from the O.R.? (http://stackoverflow.com/questions/3892999/how-to-check-if-a-control-is-child-of-another-control-control-ischildof)

  • @Rodrigoreis We support the rewriting of Stack Overflow questions or answers, provided they benefit your community. Always keep in mind, however, that automated or poorly written translations are not allowed. http://meta.answall.com/q/1/12

1 answer

1


You can use a Extension method recursive like this:

public static bool IsChildOf(this Control c, Control parent)
{
    return ((c.Parent != null && c.Parent == parent)
            || (c.Parent != null ? c.Parent.IsChildOf(parent) : false));
}

Which results in:

P.IsChildOf(ParentPanel); // true
ParentPanel.IsChildOf(P); // false

Browser other questions tagged

You are not signed in. Login or sign up in order to post.