在母版页中查找 ContentPlaceHolders

发布于 2024-09-24 23:16:18 字数 773 浏览 1 评论 0原文

我正在寻找一种动态加载母版页的方法,以便获取其中的 ContentPlaceHolders 集合。

在我可以访问它的控件之前,我希望不必加载页面对象来分配母版页,但如果这是唯一的方法,我会很乐意使用它。这就是我希望它能够工作的方式:

        Page page = new Page();
        page.MasterPageFile = "~/home.master";
        foreach (Control control in page.Master.Controls)
        {
            if (control.GetType() == typeof(ContentPlaceHolder))
            {
                // add placeholder id to collection
            }
        }

但是 page.Master 抛出空引用异常。它似乎仅在页面生命周期中创建实际页面时才加载。

我什至想过在 Page_Init() 上动态更改当前页面的 MasterPageFile,读取所有 ContentPlaceHolders 然后将原始 MasterPageFile 分配回来,但这太可怕了!

有没有办法将母版页加载到独立于实际页面的内存中,以便我可以访问它的属性?

我的最后手段可能会涉及解析 ContentPlaceHolders 的母版页内容,这不是那么优雅,但可能会更快一点。

有人可以帮忙吗?非常感谢。

I'm looking for a way to dynamically load a master page in order to get a collection of ContentPlaceHolders within.

I would prefer not to have to load a page object to assign the master page to before I can access it's controls, but if that's the only way I'll be happy to use it. This is the way I was hoping it would work:

        Page page = new Page();
        page.MasterPageFile = "~/home.master";
        foreach (Control control in page.Master.Controls)
        {
            if (control.GetType() == typeof(ContentPlaceHolder))
            {
                // add placeholder id to collection
            }
        }

But page.Master throws a null reference exception. It only seems to load at some point when an actual page has been created in the page lifecycle.

I even thought of dynamically changing the current page's MasterPageFile on Page_Init(), reading all ContentPlaceHolders then assigning the original MasterPageFile back, but that would be horrible!

Is there a way to load a master page into memory independent of an actual page so that I can access properties of it?

My final resort will probably involve parsing the master page contents for ContentPlaceHolders instead, which isn't as elegant but might be a bit faster.

Anyone able to help please? Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-10-01 23:16:18

您应该能够使用 LoadControl 加载母版页并枚举 Controls 集合。

  var site1Master = LoadControl("Site1.Master");

要查找内容控件,您需要递归搜索控件集合。这是一个快速而肮脏的例子。

static class WebHelper
{
  public static IList<T> FindControlsByType<T>(Control root) 
    where T : Control
  {
    List<T> controls = new List<T>();
    FindControlsByType<T>(root, controls);
    return controls;
  }

  private static void FindControlsByType<T>(Control root, IList<T> controls)
    where T : Control
  {
    foreach (Control control in root.Controls)
    {
      if (control is T)
      {
        controls.Add(control as T);
      }
      if (control.Controls.Count > 0)
      {
        FindControlsByType<T>(control, controls);
      }
    }
  }
}

以上可以按如下方式使用

  // Load the Master Page
  var site1Master = LoadControl("Site1.Master");

  // Find the list of ContentPlaceHolder controls
  var controls = WebHelper.FindControlsByType<ContentPlaceHolder>(site1Master);

  // Do something with each control that was found
  foreach (var control in controls)
  {
    Response.Write(control.ClientID);
    Response.Write("<br />");
  }

You should be able to use LoadControl to load the master page an enumerate the Controls collection.

  var site1Master = LoadControl("Site1.Master");

To find the Content Controls you will need to recursively search the Controls collection. Here is a quick and dirty example.

static class WebHelper
{
  public static IList<T> FindControlsByType<T>(Control root) 
    where T : Control
  {
    List<T> controls = new List<T>();
    FindControlsByType<T>(root, controls);
    return controls;
  }

  private static void FindControlsByType<T>(Control root, IList<T> controls)
    where T : Control
  {
    foreach (Control control in root.Controls)
    {
      if (control is T)
      {
        controls.Add(control as T);
      }
      if (control.Controls.Count > 0)
      {
        FindControlsByType<T>(control, controls);
      }
    }
  }
}

The above can be used as follows

  // Load the Master Page
  var site1Master = LoadControl("Site1.Master");

  // Find the list of ContentPlaceHolder controls
  var controls = WebHelper.FindControlsByType<ContentPlaceHolder>(site1Master);

  // Do something with each control that was found
  foreach (var control in controls)
  {
    Response.Write(control.ClientID);
    Response.Write("<br />");
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文