Best practices around "Page_PreRender" and "Page_Load" events

Discussion in 'ASP .Net' started by Max2006, Jul 23, 2007.

  1. Max2006

    Max2006 Guest

    Hi,

    Are there any best practices or recommendations around using
    "Page_PreRender" vs "Page_Load"? We are thinking to completely switch to
    Page_PreRender event to handle page's controls initialization requirements.

    I went through some asp.net page life cycle documentations (and diagrams)
    and it seems that we can safely move all Page_Load codes to Page_PreRender.
    Is there any work that we can only do in Page_Load and cannot do it in
    Page_PreRender?

    A link to an online article would greatly help.

    Thank you,
    Max
    Max2006, Jul 23, 2007
    #1
    1. Advertising

  2. Max2006

    bruce barker Guest

    only if the logic belongs there. the simplified cycle is:

    oninit - use to init controls before postback data loaded

    onload - after postback data applied to controls

    onclick/etc - server events fired

    prerender - after server events

    render - produce html

    general control init should be done in oninit. onload is used if
    controls use viewstate (bad practice - should only be done with
    intranets) or for postback data handling.


    -- bruce (sqlwork.com)


    Max2006 wrote:
    > Hi,
    >
    > Are there any best practices or recommendations around using
    > "Page_PreRender" vs "Page_Load"? We are thinking to completely switch to
    > Page_PreRender event to handle page's controls initialization requirements.
    >
    > I went through some asp.net page life cycle documentations (and diagrams)
    > and it seems that we can safely move all Page_Load codes to Page_PreRender.
    > Is there any work that we can only do in Page_Load and cannot do it in
    > Page_PreRender?
    >
    > A link to an online article would greatly help.
    >
    > Thank you,
    > Max
    >
    >
    bruce barker, Jul 23, 2007
    #2
    1. Advertising

  3. Hi Max,

    Besides Bruce's input, here's my thoughts on this:

    * Initialization code that runs in first page load (non-postback) is
    normally in Page_Load (http://support.microsoft.com/kb/317794)

    * Normally you need to register client script in PreRender (<<Developing
    Microsoft ASP.NET Server Controls and Components>>, chapter "Client Script
    - Related API"; and this also applies when you're using AJAX:
    http://weblogs.asp.net/leftslipper/archive/2006/11/13/HOWTO_3A00_-Write-cont
    rols-compatible-with-UpdatePanel-without-linking-to-the-ASP.NET-AJAX-DLL.asp
    x)


    Hope this helps.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 24, 2007
    #3
  4. Hi Max,

    I'm writing to check the status of this post. Please reply to let us know.
    Thanks.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 27, 2007
    #4
  5. Max2006

    Max2006 Guest

    Hi Walter,

    Your response helped me; however I was looking for an actual best practices
    guidelines. From you post I have a feeling that there is no official
    guideline?

    Thank you,
    Max



    "Walter Wang [MSFT]" <> wrote in message
    news:qRWgrm$...
    > Hi Max,
    >
    > I'm writing to check the status of this post. Please reply to let us know.
    > Thanks.
    >
    >
    > Regards,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    Max2006, Jul 30, 2007
    #5
  6. Max2006

    ThunderMusic Guest

    Hi
    What we usually do here is the following :

    /*************/
    In Page_Load : We put the initialization for the objects (like the object
    holding the logged user's infos) the postback events and pre render will
    need. No display related stuff here because it may be changed by the server
    events. Actually, we could move almost all the code from here to OnInit, but
    we are doing it in Page_Load just to make it all in one place. Actually, the
    only reason I see to put this stuff in the Page_Load is if you depend on
    data from your page like a hidden field or something (to know where you're
    at, like the id of what you are editing)
    /*************/
    In PreRender : It's like "Ok, I'm in that exact state, so what do I display
    and how do I display it?"
    /*************/

    These are not "official" guidelines, but it works pretty well in all
    situations.

    I hope it helps

    ThunderMusic

    "Max2006" <> wrote in message
    news:...
    > Hi,
    >
    > Are there any best practices or recommendations around using
    > "Page_PreRender" vs "Page_Load"? We are thinking to completely switch to
    > Page_PreRender event to handle page's controls initialization
    > requirements.
    >
    > I went through some asp.net page life cycle documentations (and diagrams)
    > and it seems that we can safely move all Page_Load codes to
    > Page_PreRender. Is there any work that we can only do in Page_Load and
    > cannot do it in Page_PreRender?
    >
    > A link to an online article would greatly help.
    >
    > Thank you,
    > Max
    >
    ThunderMusic, Jul 30, 2007
    #6
  7. Thanks ThunderMusic for the input.

    Hi Max,

    I think the "official" guidelines are scattered in following places:

    * ASP.NET page life cycle
    (http://msdn2.microsoft.com/en-us/library/ms178472.aspx)
    * Crash courses on creating controls
    (http://msdn2.microsoft.com/en-us/library/aa530687.aspx)

    Of course, the official documentation on creating controls is already the
    best place to look up information:

    #Creating Controls
    http://msdn2.microsoft.com/en-us/asp.net/aa336658.aspx


    Hope this helps.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jul 31, 2007
    #7
  8. Max2006

    ThunderMusic Guest

    Hi,
    for an ASP.NET Page cycle, we found this page to be REALLY useful when we
    developped our controls.
    http://weblogs.asp.net/jeff/archive/2004/07/04/172683.aspx

    I hope it helps

    ThunderMusic

    "Walter Wang [MSFT]" <> wrote in message
    news:...
    > Thanks ThunderMusic for the input.
    >
    > Hi Max,
    >
    > I think the "official" guidelines are scattered in following places:
    >
    > * ASP.NET page life cycle
    > (http://msdn2.microsoft.com/en-us/library/ms178472.aspx)
    > * Crash courses on creating controls
    > (http://msdn2.microsoft.com/en-us/library/aa530687.aspx)
    >
    > Of course, the official documentation on creating controls is already the
    > best place to look up information:
    >
    > #Creating Controls
    > http://msdn2.microsoft.com/en-us/asp.net/aa336658.aspx
    >
    >
    > Hope this helps.
    >
    >
    > Regards,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    ThunderMusic, Jul 31, 2007
    #8
    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. karim
    Replies:
    0
    Views:
    444
    karim
    Jul 13, 2003
  2. Replies:
    3
    Views:
    11,935
    albert braun
    Dec 17, 2005
  3. HankC
    Replies:
    3
    Views:
    425
    anton muhin
    Oct 14, 2003
  4. John Dalberg
    Replies:
    3
    Views:
    560
    samuelhon
    Nov 16, 2006
  5. Chicken McNuggets

    Best book on C gotchas and best practices?

    Chicken McNuggets, Jul 31, 2013, in forum: C Programming
    Replies:
    9
    Views:
    256
    Fred J. Tydeman
    Aug 5, 2013
Loading...

Share This Page