ASP.NET 中自定义组合中的子控件初始化
我正在开发的一系列控件中的一部分显然涉及将其中一些控件集中到复合材料中。 我很快开始了解到这是需要考虑的(这对我来说是全新的!):)
我基本上有一个 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
中时,一切都变得非常错误!
所以,看起来我在 StyledWindow
或 ChildControl
中丢失了一些东西。 有什么想法吗?
更新
为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,我开始玩了,我发现我的控件实例化有问题,因为 Longhorn 是对的,我应该能够在
OnLoad
创建脚本引用(但我不能),而 Brad是正确的,因为我需要通过添加到组合的Controls
集合来确保我的Controls
层次结构得到维护。所以,我在这里有两件事:
Controls
属性访问器,以返回此Panel
的 Controls 集合,因为我不想去ctl.Controls[0].Controls[0]
以获得我想要的实际控件。 我已经删除了它,但我需要对其进行排序。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 myControls
hierarchy was maintained by adding to theControls
collection of the composite.So, I had two things here:
Controls
property accessor for the composite to return thisPanel
's Controls collection since I dont want to have to goctl.Controls[0].Controls[0]
to get to the actual control I want. I have removed this, but I need to get this sorted.Panel
to theControls
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 thePanel
, rather than thePanel
itself?我没有看到您将控件添加到任何地方的 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).
我总是将 JavaScript 调用放在 OnLoad 函数上。 比如下面这样。
如果您仍然想进行渲染,那么您可以在响应中编写脚本。 这就是 RegisterScriptBlock 的作用,它只是将脚本内联到页面上。
I have always put the JavaScript calls on the OnLoad Function. Such as below.
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.
解决了!
好吧,我今天就下定决心一定要破解这个! 以下是我的想法:
Panel
有点黑客行为,所以我应该删除它并找出它是如何真正完成的。MyCtl.Controls[0].Controls
之类的操作来访问添加到组合中的控件。因此,我进行了搜索并点击了 MSDN,这篇文章真的很有帮助(即几乎是复制粘贴,并且解释得很好) - MSDN 传统上不擅长的事情)。 好的!
因此,我放弃了
Panel
的使用,基本上按照这篇文章进行操作,并将其视为福音,边写边做笔记。这就是我现在所拥有的:
例如,这里是模板化控件的 ASPX 标记:
代码
这是我现在在 TemplateControl.cs 中的
...然后,为了提供对 [Surrogate] 对象的控件的轻松访问:(
这就是为什么我们需要清除/add to the base.Controls)
差不多就这样了,只要你知道怎么做就很容易! :)
下一步: 设计时区域支持!
Solved!
Right, I was determined to get this cracked today! Here were my thoughts:
Panel
was a bit of a hack, so I should remove it and find out how it is really done.MyCtl.Controls[0].Controls
to access the controls added to the composite.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:
For example, here is the ASPX markup for a templated control:
Heres the Code I Have Now
In TemplateControl.cs...
Then, to provide easy access to the Controls of the [Surrogate] Object:
(this is why we needed to clear/add to the base.Controls)
And that is pretty much it, easy when you know how! :)
Next: Design Time Region Support!