2014-02-28 76 views
-1

我已经把我的模板页面上的用户控件,比如Test.aspx的无法找到用户控件在母版页

<site:Control1 runat="server" id="Bigbox" AdType="bigbox" AdTile="1" ServeOop="True"/> 

而且在控制1的后面的代码,它试图寻找另一个用户控件放在母版页上。现在

if (this.Page.Master.FindControl("Control2") != null), 

,我们在设计网站的方式是,我们有模板页面调用调用另一个母版页母版页,我的意思是这Test.aspx的

,它具有

MasterPageFile="~/Views/CMSTemplates/Shows/Show.master" 

和Show.master,它具有

MasterPageFile="~/Views/CMSTemplates/Wrapper.master" 
在Wrapper.master

,它具有现在

,是的Site.Master在根级别的母版页,所有模板提及这一点。

但是当我把控制2上的Site.Master

<site:Control2 runat="server" id="Control2"/>, 

如果(this.Page.Master.FindControl(“控制2”)将返回null。我理解,因为是的Site.Master几个层次,从测试了的.aspx,所以我试图把控制2在Show.master Wrapper.master,对照1仍然未能找到控制2。

我甚至试图把@masterType指令,仍然是行不通的

任何意见或建议?

请指教。

感谢

回答

0

我使用的模板页面上的< @mastertype参考。我使用Page.Master.Master去链上找到控件

2

你需要寻找的ID,而不是类名控制:

if (this.Page.Master.FindControl("DfpAdManager") != null) 

或者使用OfType

this.Page.Master.Controls.OfType<Control2>() 
+0

我正在通过ID查找它们。 – doglin

+1

不,你不是。 “Control2”不是ID。 “DfpAdManager”是ID。 –

0

您也可以尝试检查或搜索控件来自Page.Master.Parent,具体取决于您在层次结构中何处放置要搜索的控件,以及从哪里开始执行FindControl()。 你可以去Page.Master.Parent.Parent。如果需要的话。

+0

基本上我的母版页是嵌套的。所以我尝试了Page.Master.master ...你是否建议我应该尝试Page.master.parent.parent来代替? – doglin

相关问题