Does ASP.Net call Dispose on the Page/Controls in a page, or must I do this? Does ASP.Net call Dispose on the Page/Controls in a page, or must I do this? asp.net asp.net

Does ASP.Net call Dispose on the Page/Controls in a page, or must I do this?


It's done for you. Look at UnloadRecursive() from System.Web.UI.Control in Reflector, which is called by ProcessRequestCleanup().


No, you should not call Dispose on controls, that is being done. You are responsible for other Disposable objects you create outside the Control structure (FileStreams etc).

This follows from a general .NET principle: The Page is the owner of the Controls and therefore required to cascade the (explicit) Dispose to them. For the actual code you will have to Reflector the code for Web.UI.Control.


This article on The ASP.NET Page Life Cycle states that:

"Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed."

I would take that "any cleanup" means disposal of controls etc. I can't imagine that the designers of the ASP.NET framework would have overlooked that and nobody would have noticed.