tags:

views:

143

answers:

2

Hi! Do you know any drawback to add controls to a page on PreRender event? please don't answer 'depends on your case' I'm talking in general:-)

+3  A: 

The PreRender event happens after control events, so the control could not use any events.

If you for example add a Button in Page_PreRender, it's too late to hook up a Click event handler for it. At postack the button would not be recreated until after the click event had already been handled (and ignored).

Guffa
+1  A: 

Yes, see this link for the ASP.NET lifecycle:

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

I would recommend adding controls on the Init event as the new control would otherwise be cleared on any postbacks. This is as per http://www.4guysfromrolla.com/articles/092904-1.aspx.

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

David Neale