覆盖 WebControl 上的控件集合
我试图重写继承自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.