ASP.NET WebControl 和呈现包含的子项
我希望创建一个可以在其中容纳标记的 WebControl,并自行动态创建子控件。我遇到的问题是,我(还)无法将标记中包含的控件(参见下面的示例)与我创建的子控件分开呈现。
我知道我需要使用这两个标志设置类:
[ParseChildren(false)]
[PersistChildren(true)]
public class OuterControl : WebControl
{
...
}
示例标记如下所示:
<custom:OuterControl>
<asp:TextBox ...>
<custom:OuterControl>
在 RenderContents() 内,我需要将一些控件添加到控件树、渲染中,然后渲染包裹在特定部分的标记中的内容。例如:
protected override void RenderContents(HtmlTextWriter output)
{
EnsureChildControls();
[ Misc work, render my controls ]
[** Would like to render wrapped children here **]
[ Possibly other misc work ]
}
如上所述,我可以通过调用 RenderChildren() 让代码创建的控件呈现两次,或者通过删除该行来使包装的控件根本不呈现。该死。
想法?
I'm looking to create a WebControl that can house markup inside it, and do its own dynamic creation of child controls. The issue I'm having is that I can't (yet) render the controls housed in the markup (see example below) separate from the child controls I create.
I'm aware I need to set up the class with these 2 flags:
[ParseChildren(false)]
[PersistChildren(true)]
public class OuterControl : WebControl
{
...
}
And sample markup would look like:
<custom:OuterControl>
<asp:TextBox ...>
<custom:OuterControl>
Inside RenderContents(), I have some controls I need to add to the control tree, render, then render the ones wrapped in markup in a particular part. E.g.:
protected override void RenderContents(HtmlTextWriter output)
{
EnsureChildControls();
[ Misc work, render my controls ]
[** Would like to render wrapped children here **]
[ Possibly other misc work ]
}
As stated, I can either get my code-created controls to render twice from calling RenderChildren(), or the wrapped controls to not render at all by removing that line. Darn.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我有类似的要求(围绕提供的控件构建一组标准控件)时,我最终做了这样的事情:
这已经保持得很好。
When I had a similar requirement (building a standard set of controls around the controls supplied), I ended up doing something like this:
Which has held up quite well.