views:

103

answers:

1

Hello All,

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!

+1  A: 

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.

Rex M