views:

412

answers:

1

Lets pretend that for some reason I want to create a custom control that is derived from Control and not WebControl. Let us also assume that I need to process attributes (i.e. implement IAttributeAccessor) and that I want to do so by using an AttributeCollection just like WebControl does.

WebControl implements the Attributes property like this:


public AttributeCollection Attributes
{
    get
    {
        if (this.attrColl == null)
        {
            if (this.attrState == null)
            {
                this.attrState = new StateBag(true);
                if (base.IsTrackingViewState])
                {
                    this.attrState.TrackViewState();
                }
            }
            this.attrColl = new AttributeCollection(this.attrState);
        }
        return this.attrColl;
    }
}

Note the following:

* You cannot create an AttributeCollection without giving it a StateBag.
* We have to create a new StateBag.  It is not wise to reuse the controls StateBag because an attribute may have the name as a value stored by the control.
* We cannot call TrackViewState on the StateBag because this is an internal method.
* StateBag is a sealed class.

So as I understand it if I want to use an AttributeCollection I have to use a new StateBag which can never (without resorting to tricks like reflection) actually manage state correctly.

Am I missing something?

+1  A: 

To call TrackViewState on a custom StateBag, you have to cast it to its interface.

StateBag mybag = new StateBag();
(mybag as IStateManager).TrackViewState();

I'm guessing this design decision was made to hide the implementation of ViewState from consumers. There is some information about implementing custom state tracking on the documentation page for IStateManager.

womp
It is a call to the StateBag's TrackViewState which is internal.
drs9222
Hey, sorry for the late reply. I've updated my answer.
womp
Its sad but I was actually more confused by the hiding of the interface I didn't catch the explicit implementation of the interface although I should have inferred it from the fact that they were able to make it internal. Thanks for setting me straight.
drs9222