HttpContext.Items[] vs Session[]
Since .NET first became available, passing data around during a request has become a lot easier. The ability to set a property has made that so. Still, there are times when setting a property just won’t do the trick.
One such time is getting data from the middle tier back up to the view separate from a DataBinding operation. That is, you databind a control to the middle tier and that method needs to set a value that will be used elsewhere in the view, not in the item that is being bound.
The natural, obvious tendency is to set a session variable. But there is a better way.
The problem with session variables is that they have to be cleaned up manually or they will hang around longer than we actually need them. This will use up more session memory than is required and can potentially cause side effects that will be difficult to debug.
Instead you can use the Items[] collection that is part of the HttpContext class. It works the same as a session variable, but it only hangs around for the duration of the request. Once the information is sent back to the browser, the variables that were set in the Items[] collection go away.
You might set your variable in the middle tier like this:
HttpContext.Current.Items["myVar"] = "Some Data Here";
And retrieve it later like this:
string myVar = (string)(HttpContext.Current.Items["myVar"]);
Other Related Items:
Unhooked: How Young Women Pursue Sex, Delay Love and Lose at BothFeatures a new Afterword for this edition. A controversial look at today's sexual hook-up culture, and "[a] book...you won't stop talking about."-Patr... Read More >
Car Charger Power Adapter Cord for Magellan Maestro 3100 3140 4000 4040 4050 & Crossover GPS By Chargercity (9ft Coiled Vehicle Power Cable for Longer Reach & Easy Storage) **Item comes with ChargerCity Manufacture Direct Replacement Warranty**ChargerCity Vehicle Charger power cable for Magellan Maestro 3100 3140 4000 4040 4050 GPS
Monday Morning Leadership: 8 Mentoring Sessions You Can't Afford to MissMonday Morning Leadership is a story that can help your career! Everyone likes a good story, especially if there are lessons that can be immmediately... Read More >
If you're new here, you may want to subscribe to the mailing list to get notifications of new post and a virtual tour of past topics. Thanks for visiting!










I think it will be better if you use the ViewState object,in case you want to work in the page level.
The advantages of it, the value of the object exists while Post Backs.
@Mahmoud
Did you even read the article?
Tell me how using ViewState will allow me to pass variables around during the request.