Click here to Skip to main content
13,404,844 members (44,627 online)
Rate this:
Please Sign up or sign in to vote.
See more:

why this code write here in Page_PreRender()
and why not in page_Load() for example
code here :

void Page_PreRender()
foreach (BaseValidator valControl in Page.Validators)
WebControl assControl =
if (!valControl.IsValid)
assControl.BackColor = System.Drawing.Color.Yellow;
assControl.BackColor = System.Drawing.Color.White;
Posted 17-Dec-12 20:57pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The code is in the Page_PreRender method as at this stage in the page lifecycle, all the controls on the page that are needed have been created. This isn't true for the Page_Load event.

Have a look here for more information[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

It depends on your requirements.

Page Load : Perform actions common to all requests, such as setting up a database query. At this point, server controls in the tree are created and initialized, the state is restored, and form controls reflect client-side data.

Prerender :Perform any updates before the output is rendered. Any changes made to the state of the control in the prerender phase can be saved, while changes made in the rendering phase are lost.

ASP.NET Application and Page Life Cycle[^]
ASP.NET Page Life Cycle Overview[^]
Control Execution Lifecycle[^]
Difference between page_init, page_load and page_prerender events[^]
Page Events[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180221.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100