tags:

views:

147

answers:

2

Where in the page life cycle is it most appropriate to set event handler delegates for events raised by custom User Controls?

I have a ReportFilter user control that raises an ApplyFilterClicked event. Currently I am just using Page_Load to assign a handler method.

reportFilter.ApplyFilterClicked += reportFilter_ApplyFilterClicked;
+1  A: 

If you are creating your user controls dynamically, then the most appropriate place is in the Init phase, right where the controls are created (or should be).

Otherwise, the Load phase will work just fine, and is probably where most people set them. Obviously, you can't set the handlers anywhere later than that, otherwise they would never be called, since the event handling phase is next in line after Load.

womp
A: 

Usually the init phase is best for creating controls because this will help with viewstate updates to the controls. Check out this page for some good info on page lifecycle:

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

Hope this helps

Richard