views:

143

answers:

2

What is the best practice to dispose IDisposable members of an ASP.net page?

Should I, ...

  • use the Page_Unload(object sender, EventArgs e) event
  • override the void Dispose() method of the Control class
  • something else ...

??

+3  A: 

Ideally there shouldn't really be any. In an ASP.NET page you should really use the using(...) { ... } construct, which disposes objects immediately. If you have members of your page that need disposing, that's probably an indication of bad design, so consider putting those objects in things like session state or the cache instead.

Neil Barnwell
I'm not sure I understand your assertion that holding a disposable member is an indication of bad design?
AnthonyWJones
+2  A: 

If you absolutely have to have IDisposable objects as members of the page then I would go with during the Unload phase:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Kragen