Master.FindControl 之后动态添加内容块到母版页失败

发布于 2024-07-09 03:24:08 字数 1576 浏览 6 评论 0原文

我遇到了一个奇怪的问题,对我来说没有任何意义。 我正在尝试在页面上动态设置 MasterPage 内容控件。 我让它与以下代码配合得很好:

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        MasterPageFile = "~/MasterPages/Default.master";

        string existantContentPlaceHolderID = "ContentPlaceHolder1";
        string nonExistantContentPlaceHolderID = "foo";

        //Control c = Master.FindControl(existantContentPlaceHolderID);
        //Control c1 = Master.FindControl(nonExistantContentPlaceHolderID);

        TextBox t = new TextBox
                        {
                            Text = "Text"
                        };

        ITemplate iTemplate = new GenericITemplate(container => container.Controls.Add(t));

        AddContentTemplate(existantContentPlaceHolderID, iTemplate);

    }

    public delegate void InstantiateTemplateDelegate(Control container);

    public class GenericITemplate : ITemplate
    {
        private readonly InstantiateTemplateDelegate m_instantiateTemplate;

        public void InstantiateIn(Control container)
        {
            m_instantiateTemplate(container);
        }

        public GenericITemplate(InstantiateTemplateDelegate instantiateTemplate)
        {
            m_instantiateTemplate = instantiateTemplate;
        }
    }

这很好用,除了我希望能够在调用 AddContentTemplate 之前仔细检查 MasterPage 上是否存在 contentPlaceHolderID,因为如果添加指向的内容控件,页面将抛出错误不存在的 ContentPlaceHolder。

我遇到的问题是,在上面的示例中,当我调用注释的 Master.FindControl 行之一时,文本框不再呈现。

有谁知道为什么会这样......我无法弄清楚正在发生的事情。

谢谢, 最大限度

I've encountered an odd problem that doesn't make any sense to me. I am trying to dynamically set up MasterPage Content controls on a page. I have it working nicely with the following code:

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        MasterPageFile = "~/MasterPages/Default.master";

        string existantContentPlaceHolderID = "ContentPlaceHolder1";
        string nonExistantContentPlaceHolderID = "foo";

        //Control c = Master.FindControl(existantContentPlaceHolderID);
        //Control c1 = Master.FindControl(nonExistantContentPlaceHolderID);

        TextBox t = new TextBox
                        {
                            Text = "Text"
                        };

        ITemplate iTemplate = new GenericITemplate(container => container.Controls.Add(t));

        AddContentTemplate(existantContentPlaceHolderID, iTemplate);

    }

    public delegate void InstantiateTemplateDelegate(Control container);

    public class GenericITemplate : ITemplate
    {
        private readonly InstantiateTemplateDelegate m_instantiateTemplate;

        public void InstantiateIn(Control container)
        {
            m_instantiateTemplate(container);
        }

        public GenericITemplate(InstantiateTemplateDelegate instantiateTemplate)
        {
            m_instantiateTemplate = instantiateTemplate;
        }
    }

This works great, except I want to be able to double-check that the contentPlaceHolderIDs exist on the MasterPage before calling AddContentTemplate as the Page will throw an error if you add a Content control that points to a non-existing ContentPlaceHolder.

The problem I am having is that in the above example when I call one of the commented Master.FindControl lines, the TextBox no longer renders.

Does anyone have any ideas why this might be... I cannot makes heads or tails of what is going on.

Thanks,
Max

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

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

发布评论

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

评论(1

青春如此纠结 2024-07-16 03:24:08

问题是 AddContentTemplate 只是将其参数记录在哈希表中,准备在创建时与母版页实例组合。 创建母版页后调用它不会执行任何操作,并且读取 Master 属性会导致创建母版页。

我能看到的解决此问题的最佳方法是使用 LoadControl 创建母版页的单独实例,您可以在不影响页面自己的 Master 属性的情况下检查该实例...

MasterPage testMaster = (MasterPage) LoadControl( MasterPageFile );
Control c = testMaster.FindControl(existantContentPlaceHolderID);

创建第二个实例会产生一些开销,但对于我是否值得担心。

The problem is that AddContentTemplate just records its parameters in a hashtable ready to be combined with the master page instance when it is created. Calling it after the master page has been created won't do anything, and reading the Master property causes the master page to be created.

The best way I can see around this is to create a separate instance of the master page with LoadControl, which you can inspect without affecting the page's own Master property...

MasterPage testMaster = (MasterPage) LoadControl( MasterPageFile );
Control c = testMaster.FindControl(existantContentPlaceHolderID);

There's some overhead in creating a second instance, but it's not immediately obvious to me whether it will be worth worrying about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文