Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding controls to page on PreRender

Tags:

asp.net

events

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:-)

like image 790
StackOverflower Avatar asked Mar 24 '10 13:03

StackOverflower


2 Answers

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).

like image 107
Guffa Avatar answered Oct 14 '22 23:10

Guffa


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 https://web.archive.org/web/20210330142645/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.'

like image 20
David Neale Avatar answered Oct 14 '22 22:10

David Neale