Should I add control event handlers programmatically within Page_Init?
I want to add event handlers programmatically to the server controls rather than using their predefined OnClick
p开发者_运维技巧roperties, etc. But which would be considered a better practice for defining handlers:
- Define them in
Page_Init
- Define them in
Page_Load
...and why?
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.
精彩评论