Which page load fires first? ASPX or ASCX?

C

cmay

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?
 
C

cmay

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
 
D

Daniel Jorge

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
 
C

cmay

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
 
Y

Yahoo

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top