覆盖 WebControl 上的控件集合

发布于 2024-07-17 08:39:29 字数 308 浏览 6 评论 0原文

我试图重写继承自 WebControl 的控件的 Controls 集合,以便当用户将控件添加到 WebControl 时,我可以在它之前和之后放置按钮,并将其放入自己的包装器中。 像这样的事情:

protected override ControlCollection Controls
{
    EnsureChildControls();
    return this._panel.Controls;
}

但是,当我这样做时,由于某种原因,子控件的视图状态不会得到维护。 有任何想法吗?

谢谢!

I am trying to override the Controls collection of a control that inheirts from WebControl so that when a user adds controls to the webcontrol I can put buttons before and after it and kind of put it in its own wrapper. Something like this:

protected override ControlCollection Controls
{
    EnsureChildControls();
    return this._panel.Controls;
}

However, when I do this the viewstate of the child controls are not maintained for some reason. Any ideas?

Thanks!

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

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

发布评论

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

评论(1

娇纵 2024-07-24 08:39:29

ViewState 是一个与控件树并行构建的对象图,因此这些类型的更改往往会产生您所描述的副作用。 可能有人会说,您本质上是劫持了 Controls 对象并赋予它不同的含义,这并不是覆盖的真正目的。 相反,覆盖是为了提供不同的机制来实现相同的含义或意图。 “我的一个孩子的子控件”“我的子控件”不同。

有更好的方法来完成您所描述的内容。 一种可能是在控件上显式公开一个属性,该属性是用户应将其控件添加到的“区域”。 如果目的是使其成为一个复杂的控件,其中有一个可自定义的区域,那么就公开该区域。

The ViewState is an object graph which is built in parallel to the control tree, so these kinds of changes tend to have the side effects you're describing. It could be argued you are essentially hijacking the Controls object and giving it a different meaning, which is not really the intention of override. Rather, override is for providing a different mechanism to accomplish the same meaning or intent. "The child controls of one of my children" is not the same as "My child controls".

There are better ways to accomplish what you're describing. One could be to explicitly expose a property on your control which is the "Area" users should add their controls to. If the intent is for this to be a complex control, which has one customizable area, then expose that area as such.

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