Which page load fires first? ASPX or ASCX?

Discussion in 'ASP .Net Web Controls' started by cmay, Jul 20, 2005.

  1. cmay

    cmay Guest

    Does the Page Load event for an ASPX page always fire after the Page
    Load event for an ASCX control?

    I have a webcontrol (ascx) which populates a listbox in it's onload
    event, but it seems that the ASPX page it is contained in fires its
    onload event first, so when you try to request information from the
    ASCX it has not yet run its Page_Load.

    Will the events always fire in this order?
     
    cmay, Jul 20, 2005
    #1
    1. Advertising

  2. cmay

    clintonG Guest

    Wilson's article [1] may help. There's more at MSDN.

    <%= Clinton Gallagher
    METROmilwaukee (sm) "A Regional Information Service"
    NET csgallagher AT metromilwaukee.com
    URL http://metromilwaukee.com/
    URL http://clintongallagher.metromilwaukee.com/

    [1] http://aspalliance.com/articleViewer.aspx?aId=134

    "cmay" <> wrote in message
    news:...
    > Does the Page Load event for an ASPX page always fire after the Page
    > Load event for an ASCX control?
    >
    > I have a webcontrol (ascx) which populates a listbox in it's onload
    > event, but it seems that the ASPX page it is contained in fires its
    > onload event first, so when you try to request information from the
    > ASCX it has not yet run its Page_Load.
    >
    > Will the events always fire in this order?
    >
     
    clintonG, Jul 21, 2005
    #2
    1. Advertising

  3. cmay

    cmay Guest

    Re: Which page load fires first? ASPX or ASCX?

    I read the article and have already tried searching the newsgroups and
    MSDN but neither really answer the underlying question.

    I understand the lifecycle of a page, but in the articles that describe
    the lifecycle they simply mention that the Page OnLoad event is raised.
    They don't say if the Page's onload fires before it's webControls
    OnLoad, or vice versa, or if you can't count on a specific order for
    those events to fire.


    The problem I am having is with an ASCX control which wraps, among
    other things, a dropdown listbox. Onload, the ASCX populates the
    listbox from the database. However, onload of the PAGE, the PAGE calls
    a method against the control to set the item in the dropdown which
    should be selected first.

    However, when the Page's onload has fired, the ASCX's onload has not
    yet fired, and so the Page tells the ASCX "your default selection
    should be ID=25", and the ASCX says "What? I haven't run my onLoad
    function yet, I don't even had any data in my dropdown."


    I'm just wondering if this is always the case. Can you bank on the
    ASCX always firing its onload AFTER the page onload?

    I am going to have to work some round about way to achieve the
    functionality I need here. Is there a best practice way to do what I
    am trying to accomplish?

    Thanks
     
    cmay, Jul 21, 2005
    #3
  4. cmay

    Daniel Jorge Guest

    Re: Which page load fires first? ASPX or ASCX?

    Hi there!

    As far as I've experienced, the Page's onLoad happens before controls'
    onLoad.
    If you have to iterate fromthe page to the control, try to explicitally
    call the onLoad method.
    If you're using a user control, don't forget to declare it in your
    ASPX's code-behind once the declaration doesn't automatically happen like
    when you drag'n drop a CustomControl (like combobox, TextBox, etc).
    Well, after forcing the ASCX's onLoad you should consider cancelling the
    onLoad process that would normally occur cus, if you clean the dropdownlinst
    and post data back into it on your onload you'll lose your selection.
    Like this

    1 - ASPX: onLoad :: call MyUserControl.onLoad(this,null) method
    2 - ASCX: onLoad :: clear and populate dropdownlist
    3 - ASPX: Set selected value for MyUserControl.MyCombo
    4 - ASPX: onLoad Ends
    5 - ASCX: onLoad takes place (normal life-cycle)
    6 - ASCX: onLoad:: clear and populate dropdownlist
    At this point you'll lose your Selected Value.
    7 - Cached Events takes place
    8 - WebControl Events takes place
    9 - and so on (...)

    I say you should give up on the onLoad thing and expose a method to
    populate the dropdown. Then you'll call it only on ASPX's "!IsPostBack"
    moment!!

    Hope I could help!

    Dan



    "cmay" <> wrote in message
    news:...
    > I read the article and have already tried searching the newsgroups and
    > MSDN but neither really answer the underlying question.
    >
    > I understand the lifecycle of a page, but in the articles that describe
    > the lifecycle they simply mention that the Page OnLoad event is raised.
    > They don't say if the Page's onload fires before it's webControls
    > OnLoad, or vice versa, or if you can't count on a specific order for
    > those events to fire.
    >
    >
    > The problem I am having is with an ASCX control which wraps, among
    > other things, a dropdown listbox. Onload, the ASCX populates the
    > listbox from the database. However, onload of the PAGE, the PAGE calls
    > a method against the control to set the item in the dropdown which
    > should be selected first.
    >
    > However, when the Page's onload has fired, the ASCX's onload has not
    > yet fired, and so the Page tells the ASCX "your default selection
    > should be ID=25", and the ASCX says "What? I haven't run my onLoad
    > function yet, I don't even had any data in my dropdown."
    >
    >
    > I'm just wondering if this is always the case. Can you bank on the
    > ASCX always firing its onload AFTER the page onload?
    >
    > I am going to have to work some round about way to achieve the
    > functionality I need here. Is there a best practice way to do what I
    > am trying to accomplish?
    >
    > Thanks
    >
     
    Daniel Jorge, Jul 21, 2005
    #4
  5. cmay

    cmay Guest

    Re: Which page load fires first? ASPX or ASCX?

    Daniel ,

    Thanks for the reply!

    One followup question...


    When the ASPX onload fires, has the viewstate already been loaded for
    my ASCX?

    I'm assuming that the answer is yes, just want to make sure.


    Chris
     
    cmay, Jul 21, 2005
    #5
  6. cmay

    Yahoo Guest

    Re: Which page load fires first? ASPX or ASCX?

    YES! it would be a huge disaster if it hadn't. First OnInit is called, then
    LoadViewState, and then Load. Personally I prefer to use the loadviewstate
    and saveviewstate functions instead of accessing the ViewState bag in
    methods and properties for storing and retrieving values, I find it a much
    cleaner (i.e. less headaches) approach. You know exacly when every value is
    loaded and saved.

    protected override void LoadViewState(object savedState)
    {
    if (savedState!=null)
    {
    object[] state = (object[])savedState;
    lastSubmitsCalendarDates = (string)state[0];
    }
    }

    protected override object SaveViewState()
    {
    object[] state = new Object[2];
    state[0] = calendarDates;
    return state;
    }


    Joe
    MCAD
    SRE (Simple Rule Engine)
    https://sourceforge.net/projects/sdsre/



    "cmay" <> wrote in message
    news:...
    > Daniel ,
    >
    > Thanks for the reply!
    >
    > One followup question...
    >
    >
    > When the ASPX onload fires, has the viewstate already been loaded for
    > my ASCX?
    >
    > I'm assuming that the answer is yes, just want to make sure.
    >
    >
    > Chris
    >
     
    Yahoo, Jul 22, 2005
    #6
  7. cmay

    cmay Guest

    Re: Which page load fires first? ASPX or ASCX?

    Thanks Joe!
     
    cmay, Jul 26, 2005
    #7
    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. Bob Barrows

    Re: Page Load Event Fires Twice

    Bob Barrows, Jul 9, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    412
    Bob Barrows
    Jul 9, 2003
  2. Steve C. Orr, MCSD

    Re: Page Load Event Fires Twice

    Steve C. Orr, MCSD, Jul 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    591
    Wray Smallwood
    Jul 10, 2003
  3. =?Utf-8?B?TWFnZGVsaW4=?=

    Page Load event fires twice

    =?Utf-8?B?TWFnZGVsaW4=?=, Jun 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    702
    =?Utf-8?B?TWFnZGVsaW4=?=
    Jun 15, 2004
  4. =?Utf-8?B?QXNhIE1vbnNleQ==?=

    Page Load Event Fires Twice

    =?Utf-8?B?QXNhIE1vbnNleQ==?=, Aug 17, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    13,642
    =?Utf-8?B?QXNhIE1vbnNleQ==?=
    Aug 21, 2004
  5. Replies:
    3
    Views:
    1,010
Loading...

Share This Page