ASP.NET 中自定义组合中的子控件初始化

发布于 2024-07-06 05:25:28 字数 1911 浏览 9 评论 0原文

我正在开发的一系列控件中的一部分显然涉及将其中一些控件集中到复合材料中。 我很快开始了解到这是需要考虑的(这对我来说是全新的!):)

我基本上有一个 StyledWindow 控件,它本质上是一个具有能力的美化的 Panel做其他的事情(比如添加边框等)。

下面是实例化其中的子控件的代码。 到目前为止,它似乎可以与普通的静态控件一起正常工作:

    protected override void CreateChildControls()
    {
        _panel = new Panel();

        if (_editable != null)
            _editable.InstantiateIn(_panel);

        _regions = new List<IAttributeAccessor>();
        _regions.Add(_panel);
    }

今天当我尝试在其中嵌套更复杂的控件时出现问题。 该控件使用对页面的引用,因为它注入了 JavaScript,使其更加敏捷和响应更快(RegisterClientScriptBlock 是我需要页面引用的唯一原因)。

现在,这导致了“object null”错误,但我将其本地化到 render 方法,该方法当然是尝试针对 [null] Page 对象调用该方法。

让我感到困惑的是,该控件作为独立的工作正常,但是当放置在 StyledWindow 中时,一切都变得非常错误!

所以,看起来我在 StyledWindowChildControl 中丢失了一些东西。 有什么想法吗?

更新

Brad Wilson 非常正确地指出,您不会看到控件被添加到 Controls 集合中。 这就是 _panel 的用途,它是为我处理这个问题的,基本上然后覆盖 Controls (我从某处的指南中得到了这个):

    Panel _panel;    // Sub-Control to store the "Content".
    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return _panel.Controls;
        }
    }

我希望这有助于澄清事物。 道歉。

更新Longhorn213的答案

对,我一直在做有些人使用控件,将一个放置在复合材料内,另一个放置在外部。 然后,我在控件生命周期中的事件重大事件中获取页面的状态并将其呈现到页面。

独立运行正常,页面按预期初始化。 然而,嵌套在 Composite 中的则不同。 它的 OnLoad 事件根本没有被触发! 所以我猜布拉德可能是对的,因为我没有正确设置控制层次结构,任何人都可以就我所缺少的内容提供一些建议吗? 面板方法还不够吗? (嗯,显然不是吗?!):D

感谢你们的帮助,非常感谢:)

Part of the series of controls I am working on obviously involves me lumping some of them together in to composites. I am rapidly starting to learn that this takes consideration (this is all new to me!) :)

I basically have a StyledWindow control, which is essentially a glorified Panel with ability to do other bits (like add borders etc).

Here is the code that instantiates the child controls within it. Up till this point it seems to have been working correctly with mundane static controls:

    protected override void CreateChildControls()
    {
        _panel = new Panel();

        if (_editable != null)
            _editable.InstantiateIn(_panel);

        _regions = new List<IAttributeAccessor>();
        _regions.Add(_panel);
    }

The problems came today when I tried nesting a more complex control within it. This control uses a reference to the page since it injects JavaScript in to make it a bit more snappy and responsive (the RegisterClientScriptBlock is the only reason I need the page ref).

Now, this was causing "object null" errors, but I localized this down to the render method, which was of course trying to call the method against the [null] Page object.

What's confusing me is that the control works fine as a standalone, but when placed in the StyledWindow it all goes horribly wrong!

So, it looks like I am missing something in either my StyledWindow or ChildControl. Any ideas?

Update

As Brad Wilson quite rightly pointed out, you do not see the controls being added to the Controls collection. This is what the _panel is for, this was there to handle that for me, basically then override Controls (I got this from a guide somewhere):

    Panel _panel;    // Sub-Control to store the "Content".
    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return _panel.Controls;
        }
    }

I hope that helps clarify things. Apologies.

Update Following Longhorn213's Answer

Right, I have been doing some playing with the control, placing one within the composite, and one outside. I then got the status of Page at event major event in the control Lifecycle and rendered it to the page.

The standalone is working fine and the page is inited as expected. However, the one nested in the Composite is different. It's OnLoad event is not being fired at all! So I am guessing Brad is probably right in that I am not setting up the control hierarchy correctly, can anyone offer some advice as to what I am missing? Is the Panel method not enough? (well, it obviously isn't is it?!) :D

Thanks for your help guys, appreciated :)

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

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

发布评论

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

评论(4

山人契 2024-07-13 05:25:28

是的,我开始玩了,我发现我的控件实例化有问题,因为 Longhorn 是对的,我应该能够在 OnLoad 创建脚本引用(但我不能),而 Brad是正确的,因为我需要通过添加到组合的 Controls 集合来确保我的 Controls 层次结构得到维护。

所以,我在这里有两件事:

  1. 我重写了组合的 Controls 属性访问器,以返回此 Panel 的 Controls 集合,因为我不想去 ctl.Controls[0].Controls[0] 以获得我想要的实际控件。 我已经删除了它,但我需要对其进行排序。
  2. 我没有将Panel添加到Controls集合中,我有现在已经完成了。

所以,它现在可以工作了,但是如何获取组合的Controls属性以返回Panel中的项目,而不是 Panel 本身?

Right, I got playing and I figured that there was something wrong with my control instantiation, since Longhorn was right, I should be able to create script references at OnLoad (and I couldn't), and Brad was right in that I need to ensure my Controls hierarchy was maintained by adding to the Controls collection of the composite.

So, I had two things here:

  1. I had overriden the Controls property accessor for the composite to return this Panel's Controls collection since I dont want to have to go ctl.Controls[0].Controls[0] to get to the actual control I want. I have removed this, but I need to get this sorted.
  2. I had not added the Panel to the Controls collection, I have now done this.

So, it now works, however, how do I get the Controls property for the composite to return the items in the Panel, rather than the Panel itself?

空城仅有旧梦在 2024-07-13 05:25:28

我没有看到您将控件添加到任何地方的 Controls 集合中,这可以解释为什么它们无法访问页面(因为它们从未正式放置在页面上)。

I don't see you adding your controls to the Controls collection anywhere, which would explain why they can't access the Page (since they've never been officially placed on the page).

北恋 2024-07-13 05:25:28

我总是将 JavaScript 调用放在 OnLoad 函数上。 比如下面这样。

protected override void OnLoad(EventArgs e)
{

    // Do something to get the script
    string script = GetScript();

    this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "SomeJavaScriptName", script);

    // Could also use this function to determine if the script has been register. i.e. more than 1 of the controls exists
    this.Page.ClientScript.IsClientScriptBlockRegistered("SomeJavaScriptName");

    base.OnLoad(e);
}

如果您仍然想进行渲染,那么您可以在响应中编写脚本。 这就是 RegisterScriptBlock 的作用,它只是将脚本内联到页面上。

I have always put the JavaScript calls on the OnLoad Function. Such as below.

protected override void OnLoad(EventArgs e)
{

    // Do something to get the script
    string script = GetScript();

    this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "SomeJavaScriptName", script);

    // Could also use this function to determine if the script has been register. i.e. more than 1 of the controls exists
    this.Page.ClientScript.IsClientScriptBlockRegistered("SomeJavaScriptName");

    base.OnLoad(e);
}

If you still want to do the render, then you can just write the script in the response. Which is what the RegisterScriptBlock does, it just puts the script inline on the page.

十年九夏 2024-07-13 05:25:28

解决了!

好吧,我今天就下定决心一定要破解这个! 以下是我的想法:

  • 我认为使用 Panel 有点黑客行为,所以我应该删除它并找出它是如何真正完成的。
  • 我不想执行类似 MyCtl.Controls[0].Controls 之类的操作来访问添加到组合中的控件。
  • 我想让这该死的东西发挥作用!

因此,我进行了搜索并点击了 MSDN这篇文章真的很有帮助(即几乎是复制粘贴,并且解释得很好) - MSDN 传统上不擅长的事情)。 好的!

因此,我放弃了 Panel 的使用,基本上按照这篇文章进行操作,并将其视为福音,边写边做笔记。

这就是我现在所拥有的:

  • 我知道我使用了错误的术语。 我应该将其称为模板化控件。 虽然模板控件在技术上是复合的,但存在明显的差异。 模板化控件可以定义添加到其中的项目的界面。
  • 模板化控件非常强大,而且一旦您熟悉它们,实际上设置起来非常快速且容易!
  • 我将更多地利用设计器支持,以确保我完全理解这一切,然后发布一篇博客文章:)
  • “模板”控件用于指定模板化数据的接口。

例如,这里是模板化控件的 ASPX 标记:

<cc1:TemplatedControl ID="MyCtl" runat="server">
    <Template>
        <!-- Templated Content Goes Here -->
    </Template>
</cc1:TemplatedControl>   

代码

public class DummyWebControl : WebControl
{
    // Acts as the surrogate for the templated controls.
    // This is essentially the "interface" for the templated data.
}

这是我现在在 TemplateControl.cs 中的

    ITemplate _template;
    // Surrogate to hold the controls instantiated from 
    // within the template.
    DummyWebControl _owner;

    protected override void CreateChildControls()
    {
        // Note we are calling base.Controls here
        // (you will see why in a min).
        base.Controls.Clear();
        _owner = new DummyWebControl();

        // Load the Template Content
        ITemplate template = _template;
        if (template == null)
            template = new StyledWindowDefaultTemplate();
        template.InstantiateIn(_owner);

        base.Controls.Add(_owner);
        ChildControlsCreated = true;
    }

...然后,为了提供对 [Surrogate] 对象的控件的轻松访问:(

这就是为什么我们需要清除/add to the base.Controls)

    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return _owner.Controls;
        }
    }

差不多就这样了,只要你知道怎么做就很容易! :)

下一步: 设计时区域支持!

Solved!

Right, I was determined to get this cracked today! Here were my thoughts:

  • I thought the use of Panel was a bit of a hack, so I should remove it and find out how it is really done.
  • I didn't want to have to do something like MyCtl.Controls[0].Controls to access the controls added to the composite.
  • I wanted the damn thing to work!

So, I got searching and hit MSDN, this artcle was REALLY helpful (i.e. like almost copy 'n' paste, and explained well - something MSDN is traditionally bad at). Nice!

So, I ripped out the use of Panel and pretty much followed the artcle and took it as gospel, making notes as I went.

Here's what I have now:

  • I learned I was using the wrong term. I should have been calling it a Templated Control. While templated controls are technically composites, there is a distinct difference. Templated controls can define the interface for items that are added to them.
  • Templated controls are very powerful and actually pretty quick and easy to set up once you get your head round them!
  • I will play some more with the designer support to ensure I fully understand it all, then get a blog post up :)
  • A "Template" control is used to specify the interface for templated data.

For example, here is the ASPX markup for a templated control:

<cc1:TemplatedControl ID="MyCtl" runat="server">
    <Template>
        <!-- Templated Content Goes Here -->
    </Template>
</cc1:TemplatedControl>   

Heres the Code I Have Now

public class DummyWebControl : WebControl
{
    // Acts as the surrogate for the templated controls.
    // This is essentially the "interface" for the templated data.
}

In TemplateControl.cs...

    ITemplate _template;
    // Surrogate to hold the controls instantiated from 
    // within the template.
    DummyWebControl _owner;

    protected override void CreateChildControls()
    {
        // Note we are calling base.Controls here
        // (you will see why in a min).
        base.Controls.Clear();
        _owner = new DummyWebControl();

        // Load the Template Content
        ITemplate template = _template;
        if (template == null)
            template = new StyledWindowDefaultTemplate();
        template.InstantiateIn(_owner);

        base.Controls.Add(_owner);
        ChildControlsCreated = true;
    }

Then, to provide easy access to the Controls of the [Surrogate] Object:

(this is why we needed to clear/add to the base.Controls)

    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return _owner.Controls;
        }
    }

And that is pretty much it, easy when you know how! :)

Next: Design Time Region Support!

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