2011-02-10 72 views
0

我有以下環境:如何訪問屬性從頁面的ContentPlaceHolder在我的母版

  • 母版與contentPlacholder
  • 其使用該母版並實現了一個基類的多個頁面(fooPage
  • fooPage具有一定的屬性(fooProperty

現在我想這樣做

public partial class FooMaster : System.Web.UI.MasterPage 
{ 
    // this is originally from the designer-file 
    protected global::System.Web.UI.WebControls.ContentPlaceHolder ContentPlaceHolder; 

    protected override void OnPreRender(System.EventArgs e) 
    { 
     var fooPageInstance = this.ContentPlaceHolder as fooPage; 
     var fooPropertyInstance = fooPageInstance.fooProperty; 
     // TODO do something with the property 
    } 
} 

顯然這不起作用 - 但我怎麼能做到這一點?

我知道替代:用fooProperty調用從母版的方法在contentPage作爲一個參數 - 但我想,而有一個拉動的系統在這種情況下...

+0

@abatishchev:感謝您的編輯,但它是有點不需要從`System.Web.UI.MasterPage` ...我包括爲清楚起見,命名空間中刪除命名空間!順便說一句。爲什麼不給`page`大寫'P`? :) – 2011-02-10 13:55:07

回答

0
public partial class FooMaster : System.Web.UI.MasterPage 
{ 
    // this is originally from the designer-file 
    protected global::System.Web.UI.WebControls.ContentPlaceHolder ContentPlaceHolder; 

    protected override void OnPreRender(System.EventArgs e) 
    { 
     var fooPageInstance = this.ContentPlaceHolder.BindingContainer as FooPage; 
     var fooPropertyInstance = fooPageInstance.fooProperty; 
     // TODO do something with the property 
    } 
} 
相關問題