views:

277

answers:

1

I want to add event handlers programmatically to the server controls rather than using their predefined OnClick properties, etc. But which would be considered a better practice for defining handlers:

  1. Define them in Page_Init
  2. Define them in Page_Load

...and why?

+3  A: 
Page_Init

Everything that has to be maintained between page cycles should be declared in Page_Init, not Page_Load.

edit All the initialization, like adding event handlers, and adding controls should be added during initialization, as the state is saved between page cycles. Handling with the content of controls and the viewstate, should be done in Load.

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

Init

Raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.

.

Load

The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.

Use the OnLoad event method to set properties in controls and establish database connections.

Jan Jongboom
for my edification, why is that?
cori
Then in which conditions do I have to use `Page_Load` ?
Braveyard
Check my edit .
Jan Jongboom