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 (ie 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:
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?
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.