コード例 #1
0
        public void ReplaceAndWrap <T> (T controlToReplace, T controlToWrap, IStateModificationStrategy stateModificationStrategy)
            where T : Control, IReplaceableControl
        {
            ArgumentUtility.CheckNotNull("controlToReplace", controlToReplace);
            ArgumentUtility.CheckNotNull("controlToWrap", controlToWrap);
            ArgumentUtility.CheckNotNull("stateModificationStrategy", stateModificationStrategy);

            if (_memberCaller.GetControlState(controlToReplace) != ControlState.ChildrenInitialized)
            {
                throw new InvalidOperationException("Controls can only be wrapped during OnInit phase.");
            }

            if (controlToReplace.IsInitialized)
            {
                throw new InvalidOperationException("Controls can only be wrapped before they are initialized.");
            }

            controlToWrap.Replacer = this;

            _stateModificationStrategy = stateModificationStrategy;

            Control parent = controlToReplace.Parent;
            int     index  = parent.Controls.IndexOf(controlToReplace);

            //Mark parent collection as modifiable
            string errorMessage = _memberCaller.SetCollectionReadOnly(parent.Controls, null);

            parent.Controls.RemoveAt(index);
            parent.Controls.AddAt(index, this);

            //Mark parent collection as readonly
            _memberCaller.SetCollectionReadOnly(parent.Controls, errorMessage);

            _memberCaller.InitRecursive(this, parent);

            if (!parent.Page.IsPostBack)
            {
                _hasLoaded = true;
                Controls.Add(controlToWrap);
            }
            else
            {
                _controlToWrap = controlToWrap;
            }
        }