473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What is the difference between Page_Init and Page_Load?

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
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
12 17324
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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OL**************@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
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #2
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/de...singStages.asp

ASP.NET 2.0 Page LifeCycle:
http://msdn.microsoft.com/asp.net/de.../Internals.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OL**************@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
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #3
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2***************@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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OL**************@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
nj********@hotmail.com
http://www.nathansokalski.com/


Nov 19 '05 #4
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" <no***********@nowhere.com> wrote in message
news:%2***************@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


Nov 19 '05 #5
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2***************@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...?
Nov 19 '05 #6
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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@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 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
nj********@hotmail.com
http://www.nathansokalski.com/

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2***************@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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OL**************@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
nj********@hotmail.com
http://www.nathansokalski.com/



Nov 19 '05 #7
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/de.../Internals.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 Español : http://asp.net.do/foros/
======================================
"JIMCO Software" <co*******@jimcosoftware.com> wrote in message
news:%2***************@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" <no***********@nowhere.com> wrote in message
news:%2***************@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

Nov 19 '05 #8
Juan T. Llibre wrote:
re:
Page_Init always fires, postback or no.


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


Nov 19 '05 #9
This is too tough a league to bat 1.000 in... ;-)

Juan
======
"JIMCO Software" <co*******@jimcosoftware.com> wrote in message
news:Oq*************@TK2MSFTNGP10.phx.gbl...
Juan T. Llibre wrote:
re:
Page_Init always fires, postback or no.
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

Nov 19 '05 #10
Here is my code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="InitLoad.aspx.vb"
Inherits="WebApplication1.InitLoad"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>InitLoad</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="lblInitStatus" runat="server"
EnableViewState="False">Fired&nbsp;Init&nbsp;Event :&nbsp;</asp:Label><BR>
<asp:Label id="lblLoadStatus" runat="server"
EnableViewState="False">Fired&nbsp;Load&nbsp;Event :&nbsp;</asp:Label><BR>
<BR>
<asp:Button id="btnResetStatus" runat="server" Text="Reset Init &amp;
Load Status" Font-Bold="True"
Width="168px" EnableViewState="False"></asp:Button><BR>
<BR>
<asp:Button id="btnDoAPostback" runat="server" Text="Do A Postback"
Font-Bold="True" Width="104px"
EnableViewState="False"></asp:Button>
</form>
</body>
</HTML>
Public Class InitLoad

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Protected WithEvents lblInitStatus As System.Web.UI.WebControls.Label

Protected WithEvents lblLoadStatus As System.Web.UI.WebControls.Label

Protected WithEvents btnResetStatus As System.Web.UI.WebControls.Button

Protected WithEvents btnDoAPostback As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

lblInitStatus.Text &= "FIRED"

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lblLoadStatus.Text &= "FIRED"

End Sub

Private Sub btnResetStatus_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnResetStatus.Click

lblInitStatus.Text = "Fired&nbsp;Init&nbsp;Event:&nbsp;"

lblLoadStatus.Text = "Fired&nbsp;Load&nbsp;Event:&nbsp;"

End Sub

Private Sub btnDoAPostback_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDoAPostback.Click

If btnDoAPostback.BackColor.Equals(Color.Silver) Then

btnDoAPostback.BackColor = Color.Black

btnDoAPostback.BorderColor = Color.Black

btnDoAPostback.ForeColor = Color.Silver

Else

btnDoAPostback.BackColor = Color.Silver

btnDoAPostback.BorderColor = Color.Silver

btnDoAPostback.ForeColor = Color.Black

End If

End Sub

End Class

This code has two Labels and two Buttons. The two Labels let you know
whether the Init and Load events were fired. The first button resets the
Labels, and the second Button is used to perform a postback (I simply have
the button invert its BackColor and ForeColors). However, the Init event is
fired every time.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OD**************@TK2MSFTNGP15.phx.gbl...
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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@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 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
nj********@hotmail.com
http://www.nathansokalski.com/

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2***************@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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OL**************@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
nj********@hotmail.com
http://www.nathansokalski.com/



Nov 19 '05 #11
Check out the followin article in the .Net SDK:

http://msdn.microsoft.com/library/de...nLifecycle.asp

I keep a printed copy on my office wall.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
I'd rather be a hammer than a nail.

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Here is my code:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="InitLoad.aspx.vb" Inherits="WebApplication1.InitLoad"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>InitLoad</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="lblInitStatus" runat="server"
EnableViewState="False">Fired&nbsp;Init&nbsp;Event :&nbsp;</asp:Label><BR>
<asp:Label id="lblLoadStatus" runat="server"
EnableViewState="False">Fired&nbsp;Load&nbsp;Event :&nbsp;</asp:Label><BR>
<BR>
<asp:Button id="btnResetStatus" runat="server" Text="Reset Init &amp;
Load Status" Font-Bold="True"
Width="168px" EnableViewState="False"></asp:Button><BR>
<BR>
<asp:Button id="btnDoAPostback" runat="server" Text="Do A Postback"
Font-Bold="True" Width="104px"
EnableViewState="False"></asp:Button>
</form>
</body>
</HTML>
Public Class InitLoad

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Protected WithEvents lblInitStatus As System.Web.UI.WebControls.Label

Protected WithEvents lblLoadStatus As System.Web.UI.WebControls.Label

Protected WithEvents btnResetStatus As System.Web.UI.WebControls.Button

Protected WithEvents btnDoAPostback As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

lblInitStatus.Text &= "FIRED"

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lblLoadStatus.Text &= "FIRED"

End Sub

Private Sub btnResetStatus_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnResetStatus.Click

lblInitStatus.Text = "Fired&nbsp;Init&nbsp;Event:&nbsp;"

lblLoadStatus.Text = "Fired&nbsp;Load&nbsp;Event:&nbsp;"

End Sub

Private Sub btnDoAPostback_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDoAPostback.Click

If btnDoAPostback.BackColor.Equals(Color.Silver) Then

btnDoAPostback.BackColor = Color.Black

btnDoAPostback.BorderColor = Color.Black

btnDoAPostback.ForeColor = Color.Silver

Else

btnDoAPostback.BackColor = Color.Silver

btnDoAPostback.BorderColor = Color.Silver

btnDoAPostback.ForeColor = Color.Black

End If

End Sub

End Class

This code has two Labels and two Buttons. The two Labels let you know
whether the Init and Load events were fired. The first button resets the
Labels, and the second Button is used to perform a postback (I simply have
the button invert its BackColor and ForeColors). However, the Init event
is fired every time.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OD**************@TK2MSFTNGP15.phx.gbl...
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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@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 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
nj********@hotmail.com
http://www.nathansokalski.com/

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2***************@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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OL**************@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
> nj********@hotmail.com
> http://www.nathansokalski.com/
>



Nov 19 '05 #12
you geek Spencer..........lol

Mines in the drawer at least!!!!

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uD*************@tk2msftngp13.phx.gbl...
Check out the followin article in the .Net SDK:

http://msdn.microsoft.com/library/de...nLifecycle.asp

I keep a printed copy on my office wall.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
I'd rather be a hammer than a nail.

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Here is my code:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="InitLoad.aspx.vb" Inherits="WebApplication1.InitLoad"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>InitLoad</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="lblInitStatus" runat="server"
EnableViewState="False">Fired&nbsp;Init&nbsp;Event :&nbsp;</asp:Label><BR>
<asp:Label id="lblLoadStatus" runat="server"
EnableViewState="False">Fired&nbsp;Load&nbsp;Event :&nbsp;</asp:Label><BR>
<BR>
<asp:Button id="btnResetStatus" runat="server" Text="Reset Init &amp;
Load Status" Font-Bold="True"
Width="168px" EnableViewState="False"></asp:Button><BR>
<BR>
<asp:Button id="btnDoAPostback" runat="server" Text="Do A Postback"
Font-Bold="True" Width="104px"
EnableViewState="False"></asp:Button>
</form>
</body>
</HTML>
Public Class InitLoad

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Protected WithEvents lblInitStatus As System.Web.UI.WebControls.Label

Protected WithEvents lblLoadStatus As System.Web.UI.WebControls.Label

Protected WithEvents btnResetStatus As System.Web.UI.WebControls.Button

Protected WithEvents btnDoAPostback As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

lblInitStatus.Text &= "FIRED"

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lblLoadStatus.Text &= "FIRED"

End Sub

Private Sub btnResetStatus_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnResetStatus.Click

lblInitStatus.Text = "Fired&nbsp;Init&nbsp;Event:&nbsp;"

lblLoadStatus.Text = "Fired&nbsp;Load&nbsp;Event:&nbsp;"

End Sub

Private Sub btnDoAPostback_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnDoAPostback.Click

If btnDoAPostback.BackColor.Equals(Color.Silver) Then

btnDoAPostback.BackColor = Color.Black

btnDoAPostback.BorderColor = Color.Black

btnDoAPostback.ForeColor = Color.Silver

Else

btnDoAPostback.BackColor = Color.Silver

btnDoAPostback.BorderColor = Color.Silver

btnDoAPostback.ForeColor = Color.Black

End If

End Sub

End Class

This code has two Labels and two Buttons. The two Labels let you know
whether the Init and Load events were fired. The first button resets the
Labels, and the second Button is used to perform a postback (I simply
have the button invert its BackColor and ForeColors). However, the Init
event is fired every time.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OD**************@TK2MSFTNGP15.phx.gbl...
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 Español : http://asp.net.do/foros/
======================================
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@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 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
nj********@hotmail.com
http://www.nathansokalski.com/

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:%2***************@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 Español : http://asp.net.do/foros/
> ======================================
> "Nathan Sokalski" <nj********@hotmail.com> wrote in message
> news:OL**************@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
>> nj********@hotmail.com
>> http://www.nathansokalski.com/
>>
>
>



Nov 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: diadia | last post by:
string s = "hello"; const char *p = s.begin(); cout << p << endl; // print hello s = ""; char *p2= s.begin(); cout << p2 << endl; // print hello why?????
11
by: J Wang | last post by:
dear, I debug the program recently as follows. #include <sys/stat.h> int main(int argc, char *argv) { struct stat buf;
0
by: programer | last post by:
Can I use Activex (as vb) to connect database when I use vc.net.
3
by: Christian | last post by:
hi, what is the difference between the Page_Load() and OnLoad() event handlers. do they originate from a different point ?
3
by: lbolognini | last post by:
Hi everybody, sorry for the newbie question but I can't get the difference between these two code snippets. Is it that one is a delegate (the first) and that while the first ADDS behaviour the...
6
by: ad | last post by:
Hi, What is the difference between Page_init and Page_Load event?
2
by: RajW | last post by:
In ASP.NET VB, what is the difference between "Page_Load" and "Page_Init"? Thanks, /*Raj*/
5
by: tshad | last post by:
I have a PageInit.ascx that I want to put in all my pages and have it execute only once during the "not IsPostback" section. I also need it to execute first before anything else. I have it set...
0
by: RupeshDeenadayalan | last post by:
Is there any difference between a boot loader and a boot strapper.It would be better if any one can tell what operations does a general bootloader,boot strapper,BSP,Driver does???
5
by: bhushanbagul | last post by:
Hi All Please let me know exactly what difference i and g makes in oracle version i.e. when we say oracle 10g or oracle 11i Thanks Bhushan
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.