简体   繁体   English

如何从MasterPage中ContentPlaceholder中的页面访问属性

[英]How do I access properties from page in ContentPlaceholder in my MasterPage

I have following environment: 我有以下环境:

  • masterpage with a contentPlacholder 具有contentPlacholder的母版
  • multiple pages which use this masterpage and implement a base-class ( fooPage ) 使用此fooPage页并实现基类( fooPage )的多个页面
  • fooPage has a certain property ( fooProperty ) fooPage具有一定的属性( 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... 我知道另一种选择:使用fooProperty作为参数从contentPage中的masterPage调用一个方法-但在这种情况下,我想拥有一个拉式系统...

You can use MasterType attribute on master page. 您可以在母版页上使用MasterType属性。

See : http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx 请参阅: 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM