Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StateBag and Custom Controls

Status
Not open for further replies.

drs9222

Programmer
May 15, 2001
41
US
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:
Code:
public AttributeCollection Attributes
{
    get
    {
        if (this.attrColl == null)
        {
            if (this.attrState == null)
            {
                [red]this.attrState = new StateBag(true)[/red];
                if (base.IsTrackingViewState])
                {
                    [red]this.attrState.TrackViewState()[/red];
                }
            }
            this.attrColl = new AttributeCollection([red]this.attrState[/red]);
        }
        return this.attrColl;
    }
}

Note the following:
[ul]
[li]You cannot create an AttributeCollection without giving it a StateBag.[/li]
[li]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.[/li]
[li]We cannot call TrackViewState on the StateBag because this is an internal method.[/li]
[li]StateBag is a sealed class.[/li]
[/ul]

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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top