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

Answers were Sorted based on User's Feedback



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

Answer / ashutosh tripathi

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 ?    66 Yes 10 No

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

Answer / 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

More ASP.NET Interview Questions

Explain how cookies work. Give an example of cookie abuse.

0 Answers  


What is Partial PostBack in ASP.NET?

0 Answers   Viscus Infotech,


What is a session in programming?

0 Answers  


Explain method to handle error using HttpError in Web API?

0 Answers  


What is different authentication mechanisms used in ASP.NET?

0 Answers   Amdocs,


What is role-based security in asp.net?

0 Answers  


define silverlight and advantages ?

3 Answers   QA,


What is a master page and what does it do?

0 Answers  


Can more than one person use the same login?

0 Answers   MCN Solutions,


How is it possible for .NET to support many languages?

0 Answers  


What is difference b/w Data Grid in ASP.Net 1.1 and Gridview in 2.0

11 Answers   Bosch, IntraLogic,


Types of instancing properties and explain each. Tell the difference between multiuse,singleuse and globalmultiuse and which is default ?

0 Answers   DELL,


Categories