Search code examples
.netasp.netpage-lifecycleprerender

what sort of code which we could write in Page_PreRender event only?


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.


Solution

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