Last post May 06, 2018 05:24 PM by srider1969
May 06, 2018 03:48 AM|srider1969|LINK
Thank you in advance.
I have a .Net 4.6 Master Page and two User Controls being assigned to two different Place Holders. The order of using LoadControl to the Place Holder's control list doesn't change which User Control's Page_Load fires first.
Is there a way to control which control gets loaded first?
protected void Page_Load(object sender, EventArgs e)
ucMenu's Page_Load is firing first.
May 06, 2018 11:59 AM|PatriceSc|LINK
This is expected. The control hierarchy is likely what drives in which orders events are called (pMenu comes before pAccountAccess into your page ?)
IMO rather than trying to force the order for those calls, you should rather avoid writing code that depends on when another event for another control happens. What do you have in ucMenu's Page_Load that requires pAccountAccess's Page_Load to be called first?
May 06, 2018 03:37 PM|srider1969|LINK
What is expected? If I load a control first, I would expect that control to fire first.
As per the code I supplied, the names give exactly the answer to
your question. ucAccountAccess is loaded first. It sets user authentication that determines what is loaded in ucMenu.
I've never had much success with this but...
IMO, if you took a little more time to understand what was asked, you wouldn't have wasted your time. And more importantly, Mine.
Feel free to not respond in the future.
May 06, 2018 04:13 PM|mgebhard|LINK
I agree with PatriceSc, this is a design bug. Break the dependency between the User Controls. This will make code more robust.
Refactor the dependent code out of one or both of the user controls and centralize the dependent code. The standard OOP approach is adding a property, method, or constructor to the user control and pass the data. This gives control over processing order.
Another approach is using events. This keeps the dependency, which goes against SOLID principles, but will let you maintain process order. Simply, the first control fires a event when the data is ready. The second control listens for this event.
A variation is using the content page to orchestrate events. This is similar to how web forms works in general.
May 06, 2018 05:24 PM|srider1969|LINK
The question is about the code above's ucMenu Page_Load firing first, then ucAccountAccess (on PostBack) when its declared order (in Site1.Master.cs) is ucAccountAccess then ucMenu.
IMO a bigger flaw would be to assume something that isn't part of the QUESTION. There was nothing I supplied that could lead anybody make the leap to "it's a design problem".