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:
DYMO LABELWRITER 450 Label Printer,(1752264), USB, PC/MAC, Printer and Software, 51 Labels Per Minute, black/silver.Take the hassles out of label printing with the DYMO LabelWriter 450 label printer. DYMO Label enables you to create and print address, shipping, file... Read More >
Back To Basics - The Alcoholics Anonymous Beginners Meetings "Here are the steps we took..." in Four One Hour SessionsIn this book, Wally P. will take you through the Twelve Steps as they were taken by the early A.A. pioneers. You will learn how to develop a direct tw... Read More >
Omron HJ-112 Digital Pocket PedometerAn advanced technology pedometer that can be carried in your pocket, bag or be attached to your beltCounts every step, remembers 7 days of informa... 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.