What is pre-init event in ASP.NET 2.0 page life cycle?

Answer Posted / pavan

PreInit()
In this Page level event, all controls created during
design time are initialized with their default values. For
e.g., if you have a TextBox control with Text property
= “Hello”, it would be set by now. We can create dynamic
controls here.

This event occurs only for the Page class and
UserControls/MasterPages do not have this method to
override.

Sample code where you can override this method and add your
custom code:


protected override void OnPreInit(EventArgs e)

{
//custom code
base.OnPreInit(e);
}
Note that PreInit() is the only event where we can set
themes programmatically.

Special Case with MasterPages

It is important to note that Master Page is treated like a
control in the Content Pages.
So if a Page has a Master Page associated with it, then
the controls on the page will not be initialized and would
be null in this stage. Only after the Init() event starts,
you can access these controls directly from the page class.
Why?

The reason being that all controls placed in the Content
Page are within a ContentPlaceholder which is a child
control of a MasterPage. Now Master Page is merged and
treated like a control in the Content Pages. As I mentioned
earlier, all events except the Init() and Unload() are
fired from outermost to the innermost control. So PreInit()
in the Page is the first event to fire but User Controls or
MasterPage (which is itself a Usercontrol) do not have any
PreInit event . Therefore in the Page_PreInit() method,
neither the MasterPage nor any user control has been
initialized and only the controls inside the Page class are
set to their default values. Only after the Page_PreInit()
event the Init() events of other controls fire up.

Is This Answer Correct ?    15 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between the response.write() and response.output.write() methods?

499


Suppose You Want A Certain Asp.net Function Executed On Mouseover For A Certain Button. Where Do You Add An Event Handler?

632


Which is an advantage of application service providers?

477


Is data edited in the Repeater control?

569


What is state management react?

498






In your ASP.NET 2.0 web application you want to display an image that is selected from a collection of images. What approach will you use to implementing this?

549


what are configuration files?

552


What is a 404 redirect?

542


What permissions do asp.net applications posses by default?

632


What are the different properties of server control that exists?

551


What is the use of service provider?

574


Explain transparent caching with aop?

508


What is event bubbling?

590


What is boxing and unboxing in asp.net?

654


How to bind all the binding controls in a page at once in ASP.NET?

514