Page_PreInit

R

rn5a

In the article titled "ASP.NET Page Life Cycle Overview" at
http://msdn2.microsoft.com/en-us/library/ms178472.aspx, under the
topic "Life-cycle Events", for the Page Event named PreInit, it is
stated that

-------------------------------------
If the request is a postback, the values of the controls have not yet
been restored from view state. If you set a control property at this
stage, its value might be overwritten in the next event.
-------------------------------------

Can someone please explain me the first sentence with a small code
sample (maybe using just a TextBox & a Button)?

Also if a control property is set during PreInit, then upon postback,
its value will ALWAYS be overwritten in the next event rather than
MIGHT be overwritten in the next event (as stated in the second
sentence), isn't it?

Thanks
 
G

George Ter-Saakov

If page has controls then first Init of all those controls is called before
Init of the page is called.
sometimes you need to execute some lines first, before anything even
started.
Like in my case each page is checking if user has been properly logged in
and recreated the Context (like permissions, first, last names..) of the
user. (get's it from cache)

Then all controls are simply relying on that context being there.

for that method PreInit was created. PreInit is the one that called first.
Obviously if you try to manipulate controls at that stage they might
override it in the Init process.
Example is textbox. If you try to set it's value in PreInit then that value
will be overridden when controls read it's own value from PostBack stream..
(the one that browser sent responding you clicking button submit).




George.
 
L

Lloyd Sheen

George Ter-Saakov said:
If page has controls then first Init of all those controls is called
before Init of the page is called.
sometimes you need to execute some lines first, before anything even
started.
Like in my case each page is checking if user has been properly logged in
and recreated the Context (like permissions, first, last names..) of the
user. (get's it from cache)

Then all controls are simply relying on that context being there.

for that method PreInit was created. PreInit is the one that called first.
Obviously if you try to manipulate controls at that stage they might
override it in the Init process.
Example is textbox. If you try to set it's value in PreInit then that
value will be overridden when controls read it's own value from PostBack
stream.. (the one that browser sent responding you clicking button
submit).




George.

Just to add another use. Lets say you create a dynamic control. Now you
postback causing the page to try to restore the properties of the dynamic
control if it has viewstate. If you do not create the control in the
pre_init your page will fail.

LS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top