如何检查如果控制是另一个控制孩子? " Control.IsChildOf" [英] How to check if a control is child of another control? "Control.IsChildOf"

查看:130
本文介绍了如何检查如果控制是另一个控制孩子? " Control.IsChildOf"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个面板:

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

我如何检查是否 P ParentPanel 的孩子呢?有一些LINQish办法做到这一点?

How can I check if P is child of ParentPanel? Is there some LINQish way to do it?

难道还有比我提供了一个更多一些优化的方法?也许使用LINQ?

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));
}

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

这篇关于如何检查如果控制是另一个控制孩子? &QUOT; Control.IsChildOf&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆