Page_Load executes before aspx is rendered?

Discussion in 'ASP .Net' started by gnewsgroup, Jan 30, 2008.

  1. gnewsgroup

    gnewsgroup Guest

    This might have been discussed before, but I haven't been able to dig
    it out.

    I ask because I notice something interesting.

    Suppose, I have the following controls in a web form.

    <asp:DropDownList ID="DropDownList1" runat="server" > </
    asp:DropDownList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    Suppose, in Page_Load, I want to do:

    Label1.Text = DropDownList1.SelectedItem.Text;

    If I bind the DropDownList right in the aspx file through
    SqlDataSource, Label1.Text is empty when I check out the application
    in the browser.

    If I bind the DropDownList in the code behind, and in Page_Load, I do:

    BindTheDropDownList();
    Label1.Text = DropDownList1.SelectedItem.Text;

    Label1.Text does have the text value of the 0th item of the
    DropDownList.

    Therefore, I think that at Page_Load, the binding processes in the
    ASPX file are not done yet. Am I right?

    Thanks.
    gnewsgroup, Jan 30, 2008
    #1
    1. Advertising

  2. Yes. The binding of this sort takes place in the PreRender event.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "gnewsgroup" <> wrote in message
    news:...
    > This might have been discussed before, but I haven't been able to dig
    > it out.
    >
    > I ask because I notice something interesting.
    >
    > Suppose, I have the following controls in a web form.
    >
    > <asp:DropDownList ID="DropDownList1" runat="server" > </
    > asp:DropDownList>
    > <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    >
    > Suppose, in Page_Load, I want to do:
    >
    > Label1.Text = DropDownList1.SelectedItem.Text;
    >
    > If I bind the DropDownList right in the aspx file through
    > SqlDataSource, Label1.Text is empty when I check out the application
    > in the browser.
    >
    > If I bind the DropDownList in the code behind, and in Page_Load, I do:
    >
    > BindTheDropDownList();
    > Label1.Text = DropDownList1.SelectedItem.Text;
    >
    > Label1.Text does have the text value of the 0th item of the
    > DropDownList.
    >
    > Therefore, I think that at Page_Load, the binding processes in the
    > ASPX file are not done yet. Am I right?
    >
    > Thanks.
    Eliyahu Goldin, Jan 30, 2008
    #2
    1. Advertising

  3. gnewsgroup

    gnewsgroup Guest

    On Jan 30, 10:59 am, "Eliyahu Goldin"
    <> wrote:
    > Yes. The binding of this sort takes place in the PreRender event.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >


    Thank you. I think by "binding of this sort", you are referring to
    the kind of binding in aspx file through SqlDataSource?
    gnewsgroup, Jan 30, 2008
    #3
  4. Yes.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "gnewsgroup" <> wrote in message
    news:...
    On Jan 30, 10:59 am, "Eliyahu Goldin"
    <> wrote:
    > Yes. The binding of this sort takes place in the PreRender event.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >


    Thank you. I think by "binding of this sort", you are referring to
    the kind of binding in aspx file through SqlDataSource?
    Eliyahu Goldin, Jan 30, 2008
    #4
  5. gnewsgroup

    clintonG Guest

    I copied these notes out of a base class I had handy...

    // Events in ASP.NET Master and Content Pages
    // http://msdn2.microsoft.com/en-us/library/dct97kc3.aspx

    //1.) Master page controls Init event.
    //2.) Content controls Init event.
    //3.) Master page Init event.
    //4.) Content page Init event.
    //5.) Content page Load event.
    //6.) Master page Load event.
    //7.) Content controls Load event.
    //8.) Content page PreRender event.
    //9.) Master page PreRender event.
    //10.) Master page controls PreRender event.
    //11.) Content controls PreRender event.

    //http://www.dotnetspider.com/qa/Question27787.aspx
    //Page_Init (VB) OnInit (C#)
    //This is the first step in a Web form’s life cycle.
    //The server controls are loaded and initialized from the Web form’s view
    state.


    //Page_Load
    //The server controls are loaded in the Page object. View state information
    is available
    // at this point, so this is where we may put code to change control
    settings or display
    // text on the page.

    //Page_PreRender
    //The page object has been compiled and is about to be rendered.

    //Page_Unload
    //The page is unloaded from memory.

    //Page_Disposed
    //The Page object is released from memory. This is the last event in the
    life of a Page object.

    //Page_Error
    //An unhandled exception occurs.

    //Page_AbortTransaction
    //A transaction is aborted.

    //Page_CommitTransaction
    //A transaction is accepted.

    //Page_DataBinding
    //A server control on the page binds to a data source.


    "Eliyahu Goldin" <> wrote in
    message news:%...
    > Yes.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "gnewsgroup" <> wrote in message
    > news:...
    > On Jan 30, 10:59 am, "Eliyahu Goldin"
    > <> wrote:
    >> Yes. The binding of this sort takes place in the PreRender event.
    >>
    >> --
    >> Eliyahu Goldin,
    >> Software Developer
    >> Microsoft MVP
    >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >>

    >
    > Thank you. I think by "binding of this sort", you are referring to
    > the kind of binding in aspx file through SqlDataSource?
    >
    clintonG, Jan 30, 2008
    #5
  6. gnewsgroup

    gnewsgroup Guest

    On Jan 30, 3:31 pm, "clintonG" <> wrote:
    > I copied these notes out of a base class I had handy...
    >
    > // Events in ASP.NET Master and Content Pages
    > //http://msdn2.microsoft.com/en-us/library/dct97kc3.aspx
    >
    > //1.) Master page controls Init event.
    > //2.) Content controls Init event.
    > //3.) Master page Init event.
    > //4.) Content page Init event.
    > //5.) Content page Load event.
    > //6.) Master page Load event.
    > //7.) Content controls Load event.
    > //8.) Content page PreRender event.
    > //9.) Master page PreRender event.
    > //10.) Master page controls PreRender event.
    > //11.) Content controls PreRender event.
    >
    > //http://www.dotnetspider.com/qa/Question27787.aspx
    > //Page_Init (VB) OnInit (C#)
    > //This is the first step in a Web form's life cycle.
    > //The server controls are loaded and initialized from the Web form's view
    > state.
    >
    > //Page_Load
    > //The server controls are loaded in the Page object. View state information
    > is available
    > // at this point, so this is where we may put code to change control
    > settings or display
    > // text on the page.
    >
    > //Page_PreRender
    > //The page object has been compiled and is about to be rendered.
    >
    > //Page_Unload
    > //The page is unloaded from memory.
    >
    > //Page_Disposed
    > //The Page object is released from memory. This is the last event in the
    > life of a Page object.
    >
    > //Page_Error
    > //An unhandled exception occurs.
    >
    > //Page_AbortTransaction
    > //A transaction is aborted.
    >
    > //Page_CommitTransaction
    > //A transaction is accepted.
    >
    > //Page_DataBinding
    > //A server control on the page binds to a data source.
    >


    Nice documentation, now, that's clear.
    gnewsgroup, Jan 30, 2008
    #6
    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. Nick
    Replies:
    2
    Views:
    338
  2. erdem

    page_load executes twice ?

    erdem, Jul 23, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,854
    erdem
    Jul 23, 2004
  3. aspx page executes twice

    , Mar 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    387
  4. David C

    Code in Page_Load executes twice

    David C, Aug 8, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    615
  5. Replies:
    4
    Views:
    155
Loading...

Share This Page