|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Which page load fires first? ASPX or ASCX?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? 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 Show quoteHide quote "cmay" <c***@walshgroup.com> wrote in message news:1121894556.240646.152690@g49g2000cwa.googlegroups.com... > 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? > 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 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 Show quoteHide quote "cmay" <c***@walshgroup.com> wrote in message news:1121912939.352968.213350@o13g2000cwo.googlegroups.com... > 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 ,
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 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/ Show quoteHide quote "cmay" <c***@walshgroup.com> wrote in message news:1121960986.630697.163870@z14g2000cwz.googlegroups.com... > 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 >
CompareValidator Only Works On Manual Input
PropertyDescriptor in GetDataSource Render TreeViewControls in .net 2003 Repeater question Resolving datasource expression Resolving datasource expression 2.0 ascx on a 1.1 aspx? Accessing an .aspx page within the DMZ WebControl.Style and ViewState Merge method for multiple datasets |
|||||||||||||||||||||||