Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何判断控件是否来自母版页_C#_Webforms - Fatal编程技术网

C# 如何判断控件是否来自母版页

C# 如何判断控件是否来自母版页,c#,webforms,C#,Webforms,当迭代一个页面上所有控件的集合时(从page.controls和那些控件的子控件及其子控件等),如何判断控件是否来自页面的母版页 下面的方法似乎有效,但感觉有点脏。有没有更好的方法来获取这些信息 更新:抱歉,之前遗漏了一些代码 List<Control> allControls = GetAllControls(this.Page) foreach (Control c in allControls) { bool isFromMaster = c.NamingCont

当迭代一个页面上所有控件的集合时(从
page.controls
和那些控件的子控件及其子控件等),如何判断控件是否来自页面的母版页

下面的方法似乎有效,但感觉有点脏。有没有更好的方法来获取这些信息

更新:抱歉,之前遗漏了一些代码

List<Control> allControls = GetAllControls(this.Page)
foreach (Control c in allControls)
{
       bool isFromMaster = c.NamingContainer.TemplateControl.GetType().BaseType.BaseType == typeof(MasterPage);
}
List allControls=GetAllControls(this.Page)
foreach(所有控件中的控件c)
{
bool isFromMaster=c.NamingContainer.TemplateControl.GetType().BaseType.BaseType==typeof(母版页);
}
其中
GetAllControls
递归获取页面上的所有控件


谢谢页面。控件仅包含当前页面中的控件

如果要检查母版页控件,请使用:

this.Master.Controls
另一方面,如果要在页面上查找母版页控件:

IEnumerable<MasterPage> masterPageControls = this.Page.Controls.OfType<MasterPage>();
IEnumerable masterPageControls=this.Page.Controls.OfType();

尽管您只能有一个母版页关联到您的页面

给定对
控件的引用
,但您可以递归地查看
父属性:

bool IsFromMasterPage(Control control)
{
    while(control.Parent != null)
    {
        if (control.Parent is MasterPage) return true;
        control = control.Parent;
    }
    return false;
}

解决方案原来是通过母版页中的控件,不包括内容占位符的子项(因为这些子项提供了从页面本身添加的控件)

公共静态bool来自母版页(控件)
{
if(control.Page.Master!=null)
{
//获取母版页上的所有控件,不包括ContentPlaceholder中的控件
List masterPageControls=FindControlsIncludingPlaceholder子项(control.Page.Master);
bool match=masterPageControls.Contains(控件);
复赛;
}
返回false;
}    
公共静态列表FindControlsExcludingPlaceholder子项(控件父项)
{
列表控件=新列表();
foreach(父控件中的控件)
{
控件。添加(控件);
if(control.HasControls()&&control.GetType()!=typeof(ContentPlaceHolder))
{
AddRange(FindControlsIncludingPlaceholder子项(控件));
}
}
返回控制;
}

您无法访问子页面中的母版页控件,只能访问母版页本身?这是在运行时进行的,因此page.controls集合包含母版页和实际页面中的控件。来自MSDN:“在运行时,母版页与内容页合并,因此内容页代码可以访问母版页上的控件”我认为这对嵌套模板控件内的控件不起作用。看到我的答案了吗对不起,当我检查这个的时候,有点像周五下午。对于父页使用母版页的每个控件,上面的代码都返回true,因为该页的父页始终是母版页。所以这实际上并没有告诉您控件本身是来自.master还是.aspx页面。
public static bool IsFromMasterPage(Control control)
{
    if (control.Page.Master != null)
    {
        // Get all controls on the master page, excluding those from ContentPlaceHolders
        List<Control> masterPageControls = FindControlsExcludingPlaceHolderChildren(control.Page.Master);
        bool match = masterPageControls.Contains(control);
        return match;
    }
    return false;
}    

public static List<Control> FindControlsExcludingPlaceHolderChildren(Control parent)
{
    List<Control> controls = new List<Control>();
    foreach (Control control in parent.Controls)
    {
        controls.Add(control);
        if (control.HasControls() && control.GetType() != typeof(ContentPlaceHolder))
        {
            controls.AddRange(FindControlsExcludingPlaceHolderChildren(control));
        }
    }
    return controls;
}