【问题标题】:Is it possible to call a web form in multiple master pages?是否可以在多个母版页中调用 Web 表单?
【发布时间】:2017-04-15 19:46:19
【问题描述】:

我有一个 Web 表单,我希望 masterpage1 和 masterpage2 可以访问它。可能吗?如果是,如何?

提前致谢。

【问题讨论】:

  • 一个页面不能有多个母版页
  • 一个页面可以同时有多个嵌套母版页

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


【解决方案1】:

为简化起见,假设您在 Session 中存储了一个状态,该状态选择了母版页代码隐藏。在此示例中,您可以有 2 个母版页,但同时只使用其中一个:

//PreInit event of your desired page
protected override void OnPreInit(EventArgs e)
{
    if (!string.IsNullOrEmpty(Page.MasterPageFile))
    {
        if (Session["MyStatus"] == "1")
        {
            Page.MasterPageFile = "~/MyMaster1.master";
        }
        else
        {
            Page.MasterPageFile = "~/MyMaster2.master";
        }
    }
}

如果您想动态嵌套两个母版页,您可以这样做。这里同时有 2 个母版页:

protected override void OnPreInit(EventArgs e)
{
    if (!string.IsNullOrEmpty(Page.MasterPageFile))
    {
        if (Session["NestMaster"] == "1")
        {
            //setting a MasterPage to the Masterpage of the current page
            Page.Master.MasterPageFile = "~/MasterMaster.master";
        }
    }
}

【讨论】:

  • 谢谢@fubo。 :) 它真的帮了我很多。 :)
猜你喜欢
  • 1970-01-01
  • 2015-01-03
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 2012-06-24
  • 2012-01-23
  • 2023-03-21
相关资源
最近更新 更多