Search code examples
c#.netasp.netwebformsmaster-pages

How do I access properties from page in ContentPlaceholder in my MasterPage


I have following environment:

  • masterpage with a contentPlacholder
  • multiple pages which use this masterpage and implement a base-class (fooPage)
  • fooPage has a certain property (fooProperty)

Now i want to do something like

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
    }
}

Obviously this is not going to work - but how can I achieve this?

I know the alternative: call a method from the masterPage in the contentPage with fooProperty as a parameter - but i would like to rather have a pull-system in this case...


Solution

  • 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
        }
    }