2014-02-28 34 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

相關問題