ASP.NET WebControl 和呈现包含的子项

发布于 2024-08-10 06:50:54 字数 798 浏览 6 评论 0原文

我希望创建一个可以在其中容纳标记的 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 技术交流群。

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

发布评论

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

评论(1

终难遇 2024-08-17 06:50:54

当我有类似的要求(围绕提供的控件构建一组标准控件)时,我最终做了这样的事情:

EnsureChildControls();

Control[] currentControls = new Control[Controls.Count];

if (HasControls()) {
  Controls.CopyTo(currentControls, 0);
  Controls.Clear();
}

// Misc work, add my controls, etc, e.g.
Panel contentBox = new Panel { ID = "Content", CssClass = "content_border" };
// at some point, add wrapped controls to a control collection
foreach (Control currentControl in currentControls) {
  contentBox.Controls.Add(currentControl);
}

// Finally, add new controls back into Control collection
Controls.Add(contentBox);

这已经保持得很好。

When I had a similar requirement (building a standard set of controls around the controls supplied), I ended up doing something like this:

EnsureChildControls();

Control[] currentControls = new Control[Controls.Count];

if (HasControls()) {
  Controls.CopyTo(currentControls, 0);
  Controls.Clear();
}

// Misc work, add my controls, etc, e.g.
Panel contentBox = new Panel { ID = "Content", CssClass = "content_border" };
// at some point, add wrapped controls to a control collection
foreach (Control currentControl in currentControls) {
  contentBox.Controls.Add(currentControl);
}

// Finally, add new controls back into Control collection
Controls.Add(contentBox);

Which has held up quite well.

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