HttpContext.Items
You may have noticed that the Page has an items collection. This items collection can store any information, as it's a local dictionary. I could not find anywhere where the dictionary is serialized and stored, so the dictionary is only temporary and has to be reloaded on every page load. If you've dug around the .NET framework, you may have seen that the web parts manager and AJAX script manager utilize this, as it's a great way to get a reference to a component. I've even done this in my code, so in a class I may do on initialization:
this.Page.Items[typeof(MyManager)] = this;
This code exists within the web control (of type MyManager above). Any components that use MyManager can reference it via the Items collection, or it's possible to create a static method that returns:
public static MyManager GetManager(Page page)
{
return (MyManager)page.Items[typeof(MyManager)];
}
And an instance of the manager is returned to any caller. I really like this approach for certain things, but I ran into a jam; in a component I had, I tried this approach and it didn't work. I had a faulty assumption. See, the page class exposes properties named the same that exist on the System.Web.HttpContext class; in the page class, it simply returns the current context's instance of the property. I thought it was the same with the Items property in this case.
But no, it isn't; HttpContext has it's own separate Items dictionary, which you can use at a more globular level. I got myself out of a jam in a component this way; I was able to do:
if (HttpContext.Current != null)
SomeComponent comp = (SomeComponent)HttpContext.Current.Items[typeof(SomeComponent)];
And make it available in a class that isn't generally web accessible (doesn't inherit from Control and have any references to the page class, viewstate, etc).