Search code examples
c#asp.netobjectlifecyclelifetime

How long does an object in the CodeBehind live?


If I create an object in the code-behind of an .aspx page, how long can I expect that object to live? Will it live across post-backs? Could I pass it somehow to another page? Could I make it live as long as, say, the session object?

I searched the web hoping for a document explaining the life-cycle of objects created from the code-behind, and how to interact with this life-cycle; any related links would be appreciated.

By the way, I am using C# in the code-behind, but I imagine most advice targeting VB would be applicable as well.


Solution

  • The code-behind lives for the duration of the request. It will not live across post-backs. You can add values to Session if you want it across a postback. The same would apply to VB.