你好,我一直堅持這種情況很長一段時間?在這裏尋找幫助?從主頁訪問ControlsPlace持有人頁面
我已經定義了嵌套母版頁(1個爲默認Web應用程序和其他自定義母版頁引用默認的)
以下是內容頁的一小段代碼片段(說content.aspx)消耗我的自定義母版頁
在ASPX源代碼我已經包括:
和隱藏文件,即代碼。 custom.aspx.cs(這是問題所在):
ContentPlaceHolder masterContentPlaceHolder = (ContentPlaceHolder)Page.Master.Master.FindControl("MainContent"); //works well
ContentPlaceHolder nestedContentPlaceHolderHeading = (ContentPlaceHolder)masterContentPlaceHolder.FindControl("NestedMasterHeading"); //works well
Label NewsHeadLines = (Label)nestedContentPlaceHolderHeading.FindControl("lblSubSectionHeader"); //returns null?? The Control ID is all checked and is the same in the Nested Master Page.
**NewsHeadLines.Text = "Testing";** //System.NullReferenceException:
未將對象引用設置爲對象的實例。
這是因爲我嵌套我的控制(在這裏的標籤)在HTML表格,行和列?請指教?我有雙重檢查控制名稱或ID。
而不是使用FindControl你應該提供公共屬性在你的主頁,提供訪問適當的控件的屬性(f.e。'文本')。在這種情況下,你可以實現一個屬性'NewsHeadLines'。您始終在(主)頁面中直接引用其控件。所以你只需要在你可以訪問這些屬性之前將你的主人轉換爲正確的類型。 –
好主意蒂姆。錯過了那條線。日Thnx。 – Vaibhav