/// <devdoc>
/// </devdoc>
protected internal virtual void AddedControl(Control control, int index) {
if (control.OwnerControl != null) {
throw new InvalidOperationException(SR.GetString(SR.Substitution_NotAllowed));
}
if (control._parent != null) {
control._parent.Controls.Remove(control);
}
control._parent = this;
control._page = Page;
control.flags.Clear(designModeChecked);
// We only add to naming container if it is available. Otherwise, it will be pushed through
// during InitRecursive
Control namingContainer = flags[isNamingContainer] ? this : _namingContainer;
if (namingContainer != null) {
control.UpdateNamingContainer(namingContainer);
if (control._id == null && !control.flags[idNotRequired]) {
// this will also dirty the name table in the naming container
control.GenerateAutomaticID();
}
else if (control._id != null || (control._controls != null)) {
// If the control has and ID, or has children (which *may* themselves
// have ID's), we need to dirty the name table (ASURT 100557)
namingContainer.DirtyNameTable();
}
}
/*
* The following is for times when AddChild is called after CreateChildControls. This
* allows users to add children at any time in the creation process without having
* to understand the underlying machinery.
* Note that if page is null, it means we haven't been attached to a container ourselves.
* If this is true, when we are, our children will be recursively set up.
*/
if (_controlState >= ControlState.ChildrenInitialized) {
Debug.Assert(namingContainer != null);
control.InitRecursive(namingContainer);
// VSWhidbey 396372: We need to reregister the control state if the control is reparented because the control
// is unregistered during unload, but its already been inited once, so it will not get its Init called again
// which is where most controls call RegisterRequiresControlState
if (control._controlState >= ControlState.Initialized &&
control.RareFields != null &&
control.RareFields.RequiredControlState) {
Page.RegisterRequiresControlState(control);
}
if (_controlState >= ControlState.ViewStateLoaded) {
object viewState = null;
if(_occasionalFields != null && _occasionalFields.ControlsViewState != null) {
viewState = _occasionalFields.ControlsViewState[index];
// This solution takes the conservative approach that once viewstate has been
// applied to a child control, it is thrown away. This eliminates inadvertently
// setting viewstate on the wrong control, which can occur in scenarios where
// the child control collection is being manipulated via code. Probably need
// to provide a feature where programmer can control whether to reapply viewstate
// or not.
if (LoadViewStateByID) {
control.EnsureID();
viewState = _occasionalFields.ControlsViewState[control.ID];
_occasionalFields.ControlsViewState.Remove(control.ID);
}
else {
viewState = _occasionalFields.ControlsViewState[index];
_occasionalFields.ControlsViewState.Remove(index);
}
}
control.LoadViewStateRecursive(viewState);
if (_controlState >= ControlState.Loaded) {
control.LoadRecursive();
if (_controlState >= ControlState.PreRendered)
control.PreRenderRecursiveInternal();
}
}
}
}