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 check if P
is child of ParentPanel
? Is there some LINQish way to do it?
Is there some more optimized way than the one I provided? Maybe using Linq?
I end up making a recursive extension method
public static bool IsChildOf(this Control c, Control parent)
{
return ((c.Parent != null && c.Parent == parent) || (c.Parent != null ? c.Parent.IsChildOf(parent) : false));
}
Resulting in
P.IsChildOf(ParentPanel); // true
ParentPanel.IsChildOf(P); // false
You can do that search recursive:
Panel topParent = GetTopParent(P);
private Panel GetTopParent(Panel child)
{
if (child.Parent.GetType() == typeof(Panel))
return GetTopParent((Panel)child.Parent);
else return child;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With