views:

93

answers:

3

hi... I google about Page_preRender that how can we use Page_PreRender rather than page_Load or what could be the scenario where we could not use page_Load and have to use Page_PreRender but every place I find that,

"The point at which the objects are prerendered is the last time changes to the objects can be saved or persisted to viewstate. This makes the PreRender step a good place to make final modifications, such as changing properties of controls or changing Control Tree structure, without having to worry about ASP.NET making changes to objects based off of database calls or viewstate updates. After the PreRender phase those changes to objects are locked in and can no longer be saved to the page viewstate. The PreRender step can be overridden using OnPreRender".

but I could not understand that which sort of changes are which we could not do in page_Load and have to do in PreRender.

As I know that on Page_PreInit we can create Controls dynamically, change or assign MasterPage or Theme which we could not in page_Load or after Init.

But what about Page_PreRender what are things which we can only do in Page_PreRender.

A: 

It's not a question of what you can only do in Page_PreRender, there may be dependencies between your controls that only become apparent after all the events have been processed in Page_Load.

Lazarus
so what could be the scenario ... ???
Azhar
@Azhar - if you need to make changes _after_ all events have fired, this is the place.
Oded
@Oded : thanks and please could you specify that what sort of changes these could be?
Azhar
A: 

Page_PreRender event can be used to specify changes to the html rendering for your controls. One important difference between Load and PreRender is that Load runs before your control events are fired and PreRender is called after the control events. So, if on the basis of your control events code you want to change the output html, you can do so in the PreRender event.

Another important thing to note is that the Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page. So, this gives you added leverage on how to modify the rendering of a particular control. This is not possible in Page_Load.

It may also be used if you're using custom/user controls where the rendering of the control is done by you.

Sidharth Panwar
@Sidharth: thanks but for "this gives you added leverage on how to modify the rendering of a particular control" can you give some example of it.
Azhar
One simple example could be if a button click sets a session variable to a new value, you wont see the new value in Load but you would see the new value in PreRender. Based on this value you may change a label or do something else with the control.
Sidharth Panwar
In case of gridview control too, I "think" the updated values (modified due to grid commands - Update etc.) are not available at the time of Load. You can change the sorting/filtering etc. at this point if you want to. Do check about this though as I'm not 100% sure on this.
Sidharth Panwar
A: 

Certain events are triggered after the Page_Load event. Some are apparent while others are not. This is because these events can only perform after the recreation (or rather after loading). For eg, If you wish to register a piece of client script, The good place could be Page_Render. Or the Page.validate method which you might purposefully call in the Page_Load to validate controls can be understood to be already called in the render stages.

Likewise certain others which only work when their associated controls are ready to be rendered (fully treated for a user's browser).

loxxy
@loxxy: thanks but "If you wish to register a piece of client script, The good place could be Page_Render." why not Page_Load
Azhar
loxxy