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).

Comments

# dictionary » Blog Archive » HttpContext.Items

Wednesday, April 30, 2008 2:05 AM by dictionary » Blog Archive » HttpContext.Items

Pingback from  dictionary  » Blog Archive   » HttpContext.Items

# dictionary » Blog Archive » dictionary ?? Blog Archive ?? HttpContext.Items

Pingback from  dictionary  » Blog Archive   » dictionary ?? Blog Archive ?? HttpContext.Items

# dictionary » Blog Archive » dictionary ?? Blog Archive ?? dictionary ?? Blog Archive …

Pingback from  dictionary  » Blog Archive   » dictionary ?? Blog Archive ?? dictionary ?? Blog Archive …

# Ventura Toyota Car Parts, Ventura Stock Brokers

Thursday, May 20, 2010 8:59 PM by Ventura Toyota Car Parts, Ventura Stock Brokers

Pingback from  Ventura Toyota Car Parts, Ventura Stock Brokers

# B Qx9300 B Gigabit Ethernet, B300 Spare Tire Cover Dodge Dart Body Parts

Pingback from  B Qx9300 B Gigabit Ethernet, B300 Spare Tire Cover Dodge Dart Body Parts

# 2008 Chevrolet Equinox Ls Review, Chevrolet Equinox Ls

Pingback from  2008 Chevrolet Equinox Ls Review, Chevrolet Equinox Ls

# Part Data Mining, Royal Mint Mini Wales

Friday, May 21, 2010 8:43 PM by Part Data Mining, Royal Mint Mini Wales

Pingback from  Part Data Mining, Royal Mint Mini Wales

# 740i Woman, 740i Cupon 735il

Saturday, May 22, 2010 11:12 AM by 740i Woman, 740i Cupon 735il

Pingback from  740i Woman, 740i Cupon 735il

# 1 Headlight Saturn L300, Sale By Owner Saturn L300

Saturday, May 22, 2010 1:04 PM by 1 Headlight Saturn L300, Sale By Owner Saturn L300

Pingback from  1 Headlight Saturn L300, Sale By Owner Saturn L300

# 1992 Sable Accessories Air Filters Mercury Villager, Mercury Villager Radiator Leak Automobile - 378.zapstreaming.com

Pingback from  1992 Sable Accessories Air Filters Mercury Villager, Mercury Villager Radiator Leak Automobile - 378.zapstreaming.com

# Vehicross Tube I Ve, Amigo Models Isuzu Vehicross - 236.an74.com

Pingback from  Vehicross Tube I Ve, Amigo Models Isuzu Vehicross - 236.an74.com

# Samurai Warriors 2 Discount Truck Look, Samurai Warriors 2 Character You - 287.defutbolazo.com

Pingback from  Samurai Warriors 2 Discount Truck Look, Samurai Warriors 2 Character You - 287.defutbolazo.com

# Hyundai Trucks Duty, Certified Hyundai Xg300 Autozone - 104.binggreen.com

Pingback from  Hyundai Trucks Duty, Certified Hyundai Xg300 Autozone - 104.binggreen.com