What is the difference between the Page_Init and Page_Load events? When I
was debugging my code, they both seemed to get triggered on every postback.
I am assuming that there is some difference, and I would like to know what
it is so that I can take advantage of it in my code. Thanks.
--
Nathan Sokalski
njsokalski@dotnet.itags.org.hotmail.com
http://www.nathansokalski.com/Page_Init
The Page_Init event is the first to occur
when an ASP.NET page is executed.
This is where you perform any initialization steps that
you need to set up or create instances of server controls.
You can't access controls in this event because
there is no guarantee that they have been created yet.
Controls are created during this event, and you can control
whether your attempt to use these objects will be denied by
the server processing your request.
The Page_Init event fires only the first time the page is loaded.
When you postback to any page, the Page_Init event doesn't fire.
The Page_Load event fires each time the page loads, postback or not.
Page_Load
This event occurs only when all the objects on the
page have been created and are available for use.
Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Espaol : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <njsokalski@.hotmail.com> wrote in message
news:OLErlmVwFHA.2880@.TK2MSFTNGP12.phx.gbl...
> What is the difference between the Page_Init and Page_Load events? When I
was debugging
> my code, they both seemed to get triggered on every postback. I am assumin
g that there
> is some difference, and I would like to know what it is so that I can take
advantage of
> it in my code. Thanks.
> --
> Nathan Sokalski
> njsokalski@.hotmail.com
> http://www.nathansokalski.com/
>
In addition to Juan's excellent comments, if you'd like to do further
research on the subject I'd suggest you look at this documentation:
ASP.NET 1.x Page Lifecycle:
http://msdn.microsoft.com/library/d...ssingStages.asp
ASP.NET 2.0 Page LifeCycle:
http://msdn.microsoft.com/asp.net/d... />
ernals.asp
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Nathan Sokalski" <njsokalski@.hotmail.com> wrote in message
news:OLErlmVwFHA.2880@.TK2MSFTNGP12.phx.gbl...
> What is the difference between the Page_Init and Page_Load events? When I
> was debugging my code, they both seemed to get triggered on every
> postback. I am assuming that there is some difference, and I would like to
> know what it is so that I can take advantage of it in my code. Thanks.
> --
> Nathan Sokalski
> njsokalski@.hotmail.com
> http://www.nathansokalski.com/
>
That's what I thought, but in all the debugging I have done the Init event
gets fired regardless. The specific test I did was creating two labels, one
of which I modify in the Init, and one that I modify in the Load. I clear
them between postbacks using a Button.Click event handler and set
EnableViewState="false" for the Labels. I also used Visual Studio's Debug
feature, and it showed the code inside Init being executed on each postback.
Maybe I'm doing something wrong in my test, could you either tell me what
I'm forgetting or send me an example? What you said is what I thought the
difference was, but for some reason it isn't working that way for me.
Thanks.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
news:%23OY11sVwFHA.904@.tk2msftngp13.phx.gbl...
> Page_Init
> The Page_Init event is the first to occur
> when an ASP.NET page is executed.
> This is where you perform any initialization steps that
> you need to set up or create instances of server controls.
> You can't access controls in this event because
> there is no guarantee that they have been created yet.
> Controls are created during this event, and you can control
> whether your attempt to use these objects will be denied by
> the server processing your request.
> The Page_Init event fires only the first time the page is loaded.
> When you postback to any page, the Page_Init event doesn't fire.
> The Page_Load event fires each time the page loads, postback or not.
> Page_Load
> This event occurs only when all the objects on the
> page have been created and are available for use.
>
>
> Juan T. Llibre, ASP.NET MVP
> ASP.NET FAQ : http://asp.net.do/faq/
> Foros de ASP.NET en Espaol : http://asp.net.do/foros/
> ======================================
> "Nathan Sokalski" <njsokalski@.hotmail.com> wrote in message
> news:OLErlmVwFHA.2880@.TK2MSFTNGP12.phx.gbl...
>
Nathan Sokalski wrote:
> That's what I thought, but in all the debugging I have done the Init
> event gets fired regardless. The specific test I did was creating two
> labels, one of which I modify in the Init, and one that I modify in
> the Load. I clear them between postbacks using a Button.Click event
> handler and set EnableViewState="false" for the Labels. I also used
> Visual Studio's Debug feature, and it showed the code inside Init
> being executed on each postback. Maybe I'm doing something wrong in
> my test, could you either tell me what I'm forgetting or send me an
> example? What you said is what I thought the difference was, but for
> some reason it isn't working that way for me. Thanks.
> "Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
> news:%23OY11sVwFHA.904@.tk2msftngp13.phx.gbl...
> The Page_Init event fires only the first time the page is loaded.
> When you postback to any page, the Page_Init event doesn't fire.
> The Page_Load event fires each time the page loads, postback or not.
Page_Init always fires, postback or no.
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com
FrontPage add-ins for FrontPage 2000 - 2003
"Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
news:%23OY11sVwFHA.904@.tk2msftngp13.phx.gbl...
> The Page_Init event fires only the first time the page is loaded.
> When you postback to any page, the Page_Init event doesn't fire.
Er... are you sure about this...?
re:
> Maybe I'm doing something wrong in my test, could you either tell me what
I'm forgetting
> or send me an example?
I can't possibly know what you're forgetting
because I don't know what you're doing.
How about posting your code so we can, maybe, help you ?
Reduce the sample to the bare minimum needed, please.
Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Espaol : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <njsokalski@.hotmail.com> wrote in message
news:%237q02gYwFHA.1132@.TK2MSFTNGP10.phx.gbl...
> That's what I thought, but in all the debugging I have done the Init event
gets fired
> regardless. The specific test I did was creating two labels, one of which
I modify in
> the Init, and one that I modify in the Load. I clear them between postback
s using a
> Button.Click event handler and set EnableViewState="false" for the Labels.
I also used
> Visual Studio's Debug feature, and it showed the code inside Init being ex
ecuted on each
> postback. Maybe I'm doing something wrong in my test, could you either tel
l me what I'm
> forgetting or send me an example? What you said is what I thought the diff
erence was,
> but for some reason it isn't working that way for me. Thanks.
> --
> Nathan Sokalski
> njsokalski@.hotmail.com
> http://www.nathansokalski.com/
> "Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
> news:%23OY11sVwFHA.904@.tk2msftngp13.phx.gbl...
>
re:
> Page_Init always fires, postback or no.
You're right, of course.
My apologies to all for that short-circuit.
The page lifecycle table at :
http://msdn.microsoft.com/asp.net/d... />
ernals.asp
lists the ASP.NET 2.0 events that happen all the time,
and the events which only occur on postback.
Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Espaol : http://asp.net.do/foros/
======================================
"JIMCO Software" <contactus@.jimcosoftware.com> wrote in message
news:%23EdENuYwFHA.596@.TK2MSFTNGP12.phx.gbl...
> Nathan Sokalski wrote:
>
> Page_Init always fires, postback or no.
> --
> Jim Cheshire
> JIMCO Software
> http://www.jimcosoftware.com
> FrontPage add-ins for FrontPage 2000 - 2003
>
>
Juan T. Llibre wrote:
> re:
> You're right, of course.
> My apologies to all for that short-circuit.
>
Lord knows, happens to all of us. :)
Jim Cheshire
JIMCO Software
http://www.jimcosoftware.com
FrontPage add-ins for FrontPage 2000 - 2003
This is too tough a league to bat 1.000 in... ;-)
Juan
======
"JIMCO Software" <contactus@.jimcosoftware.com> wrote in message
news:Oq2$BOdwFHA.340@.TK2MSFTNGP10.phx.gbl...
> Juan T. Llibre wrote:
> Lord knows, happens to all of us. :)
> Jim Cheshire
> JIMCO Software
> http://www.jimcosoftware.com
> FrontPage add-ins for FrontPage 2000 - 2003
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment