Is there an after Page_Load event in ASP.net
Asked Answered
L

2

43

Is there an event that is triggered after all Page_Load events have completed?

How can i have more than one Page_Load?
When you have user controls.

Before my page can render, i need my page (and all embedded controls) to have initialized themselves by completing their Page_Load events.

The problem, of course, is that if i put code in my page's Page_Load handler:

MyPage.aspx
   --> Page_Load
          ---> DoSomethingWithUserControl()
UserControl1.ascx
   --> Page_Load
          ---> initialize ourselves now that viewstate has been restored

then i begin to access my UserControl1 control before it is ready.

i need a way to run code after all Page_Load events have fired, but before any postback events (e.g. Click events) have fired:

MyPage.aspx
   --> Page_Load
UserControl1.ascx
   --> Page_Load
          ---> initialize ourselves now that viewstate has been restored
MyPage.aspx
   --> Page_AfterLoad
          ---> DoSomethingWithUserControl()

Looking at the page lifecycle in MSDN it looks like there is no way to raise an event after all Page_Loads have been completed:

enter image description here

Is there a way to raise an after after all Page_Loads have completed?

Langdon answered 27/6, 2012 at 21:27 Comment(0)
E
70

Page_LoadComplete is the event that is raised after all controls have been loaded

Remember that the Init event is first triggered by all child controls, and just when all controls have been initialized, the Init event of the page is raised. The Load event works the other way around, the page first raises the Load event and then each child control raises its own Load event. At the end the LoadComplete is raised. Note that this is true only when the controls are created at design time, when the controls are created dynamically they (sadly) do not follow this approach strictly.

From MSDN:

If controls are created dynamically at run time or declaratively within templates of data-bound controls, their events are initially not synchronized with those of other controls on the page. For example, for a control that is added at run time, the Init and Load events might occur much later in the page life cycle than the same events for controls created declaratively. Therefore, from the time that they are instantiated, dynamically added controls and controls in templates raise their events one after the other until they have caught up to the event during which it was added to the Controls collection.

Take a look:

(source: http://msdn.microsoft.com/en-us/library/ms178472.aspx)

enter image description here

Edit 1

In order to fulfill all your requirements:

i need a way to run code after all Page_Load events have fired, but before any postback events (e.g. Click events) have fired:

I think the easiest way is to declare a custom event in the User Control and fire it after the control has been loaded, then jus subscribe to that event in your ASPX

User Control

    public event Action LoadCompleted = delegate { };

    protected void Page_Load(object sender, EventArgs e)
    {
        this.LoadCompleted();
    }

ASPX page

    protected void Page_Load(object sender, EventArgs e)
    {
        this.myUserControl.LoadCompleted += () => 
        {
            // do somethign interesting
            this.lblMessage.Text = DateTime.Now.ToString();
        };
    }
Elma answered 27/6, 2012 at 21:28 Comment(6)
+1 Having researched this many times in a lot of detail, I am surprised I have never seen this particular diagram before... some similar but not quite as detailed.Historiography
yeah the page life cycle is tricky, personally I hated, that's one of the reasons I love MVCElma
Love MVC to. Even feeling like I know the page life cycle inside out, I still hate to deal with ASP.NET. They tried too hard to make people feel like they were programming win forms(I love win forms though).Historiography
Good answer. But the questions states i need a way to run code after all Page_Load events have fired, but before any postback events (e.g. Click events). OnLoadComplete seems to fire after the control events...Piezochemistry
@Piezochemistry you are right... I missed that sorry, check the updated responseElma
I don't think that will work either. The delegate will be called before the child controls are loaded.Piezochemistry
P
1

It seems that there really is no way to reliably get your code to execute between the time when all (statically declared, see Jupaols comment about dynamically added) controls are loaded and the time when their postback events are raised.

I even went so far as to disassemble the Page class using reflector and as far as I could tell, the page isn't calling any methods that you could tap into between these times.

So for the general case it seems like your just out of luck.

But your statement

then i begin to access my UserControl1 control before it is ready

is questionable. By the time the Page_Load method is called, the viewstate and control state have been set on all controls (at least the statically declared ones). So unless you have some more state initialization going on in the OnLoad method of a control, the control is ready to be tinkered with. If you DO have something going on in OnLoad, then why not do the tinkering there?

Piezochemistry answered 28/6, 2012 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.