开发者

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 OnPreRe开发者_开发知识库nder(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...


You can use MasterType attribute on master page.

See : http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜