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

旧巷老猫 提交于 2019-12-13 03:46:43

问题


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


回答1:


You can use MasterType attribute on master page.

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




回答2:


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


来源:https://stackoverflow.com/questions/4956066/how-do-i-access-properties-from-page-in-contentplaceholder-in-my-masterpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!