What is the difference between Page_Init and Page_Load?

Discussion in 'ASP .Net' started by Nathan Sokalski, Sep 24, 2005.

  1. 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

    http://www.nathansokalski.com/
    Nathan Sokalski, Sep 24, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    >
    > http://www.nathansokalski.com/
    >
    Juan T. Llibre, Sep 24, 2005
    #2
    1. Advertising

  3. 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...on/html/vbconWebFormsPageProcessingStages.asp

    ASP.NET 2.0 Page LifeCycle:
    http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnvs05/html/Internals.asp

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net


    "Nathan Sokalski" <> wrote in message
    news:...
    > 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
    >
    > http://www.nathansokalski.com/
    >
    Steve C. Orr [MVP, MCSD], Sep 25, 2005
    #3
  4. 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

    http://www.nathansokalski.com/

    "Juan T. Llibre" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:...
    >> 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
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, Sep 25, 2005
    #4
  5. 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" <> wrote in message
    > news:%...
    >
    > 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
    JIMCO Software, Sep 25, 2005
    #5
  6. Nathan Sokalski

    Mark Rae Guest

    "Juan T. Llibre" <> wrote in message
    news:%...

    > 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...?
    Mark Rae, Sep 25, 2005
    #6
  7. 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" <> wrote in message
    news:%...
    > 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
    >
    > http://www.nathansokalski.com/
    >
    > "Juan T. Llibre" <> wrote in message
    > news:%...
    >> 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" <> wrote in message
    >> news:...
    >>> 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
    >>>
    >>> http://www.nathansokalski.com/
    >>>

    >>
    >>

    >
    >
    Juan T. Llibre, Sep 25, 2005
    #7
  8. 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/default.aspx?pull=/library/en-us/dnvs05/html/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" <> wrote in message
    news:%...
    > 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" <> wrote in message
    >> news:%...
    >>
    >> 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, Sep 25, 2005
    #8
  9. 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
    JIMCO Software, Sep 25, 2005
    #9
  10. This is too tough a league to bat 1.000 in... ;-)



    Juan
    ======
    "JIMCO Software" <> wrote in message
    news:Oq2$...
    > 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
    Juan T. Llibre, Sep 25, 2005
    #10
  11. 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

    http://www.nathansokalski.com/
    "Juan T. Llibre" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%...
    >> 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
    >>
    >> http://www.nathansokalski.com/
    >>
    >> "Juan T. Llibre" <> wrote in message
    >> news:%...
    >>> 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" <> wrote in message
    >>> news:...
    >>>> 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
    >>>>
    >>>> http://www.nathansokalski.com/
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Nathan Sokalski, Sep 25, 2005
    #11
  12. Check out the followin article in the .Net SDK:

    http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.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" <> wrote in message
    news:%...
    > 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
    >
    > http://www.nathansokalski.com/
    > "Juan T. Llibre" <> wrote in message
    > news:...
    >> 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" <> wrote in message
    >> news:%...
    >>> 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
    >>>
    >>> http://www.nathansokalski.com/
    >>>
    >>> "Juan T. Llibre" <> wrote in message
    >>> news:%...
    >>>> 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" <> wrote in message
    >>>> news:...
    >>>>> 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
    >>>>>
    >>>>> http://www.nathansokalski.com/
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Kevin Spencer, Sep 26, 2005
    #12
  13. you geek Spencer..........lol

    Mines in the drawer at least!!!!

    --
    Regards

    John Timney
    ASP.NET MVP
    Microsoft Regional Director

    "Kevin Spencer" <> wrote in message
    news:...
    > Check out the followin article in the .Net SDK:
    >
    > http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.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" <> wrote in message
    > news:%...
    >> 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
    >>
    >> http://www.nathansokalski.com/
    >> "Juan T. Llibre" <> wrote in message
    >> news:...
    >>> 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" <> wrote in message
    >>> news:%...
    >>>> 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
    >>>>
    >>>> http://www.nathansokalski.com/
    >>>>
    >>>> "Juan T. Llibre" <> wrote in message
    >>>> news:%...
    >>>>> 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" <> wrote in message
    >>>>> news:...
    >>>>>> 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
    >>>>>>
    >>>>>> http://www.nathansokalski.com/
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    John Timney \(ASP.NET MVP\), Sep 26, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian

    Page_Init() and Page_Load()

    Christian, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    669
    Cristian Suazo
    Jul 16, 2003
  2. Christian
    Replies:
    0
    Views:
    487
    Christian
    Jul 16, 2003
  3. jack
    Replies:
    4
    Views:
    12,351
    John Saunders
    Aug 1, 2004
  4. ad
    Replies:
    6
    Views:
    35,517
    Joerg Jooss
    Nov 16, 2005
  5. Eric
    Replies:
    1
    Views:
    208
    Evertjan.
    Jun 13, 2009
Loading...

Share This Page