【问题标题】:How do I access properties from page in ContentPlaceholder in my MasterPage如何从我的 MasterPage 的 ContentPlaceholder 中的页面访问属性
【发布时间】:2011-02-10 10:42:28
【问题描述】:

我有以下环境:

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

显然这是行不通的 - 但我怎样才能做到这一点?

我知道另一种选择:从 contentPage 中的 masterPage 调用一个方法,并使用 fooProperty 作为参数 - 但在这种情况下我宁愿有一个拉系统...

【问题讨论】:

  • @abatishchev:感谢您的编辑,但从System.Web.UI.MasterPage 中删除命名空间有点不必要...为了清楚起见,我包含命名空间!顺便提一句。为什么不给page 一个大写的P?? :)

标签: c# .net asp.net webforms master-pages


【解决方案1】:

您可以在母版页上使用MasterType 属性。

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

【讨论】:

  • -,这与问题有什么关系??
  • @Andreas:当您使用 @MasterType 指令(例如示例中的指令)时,您可以在母版页上引用成员,如下例所示:CompanyName.Text = Master.CompanyName;
  • 我在我的问题中提到过:我不想使用推送系统!问题是,我想从 masterPage 访问 contentPage 的属性(相反的方向!)
【解决方案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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多