I am afraid that the inline function does not check which phase of the Page-Lifecycle the page is using. It is also difficult to add this functionality without processing all the events of the page itself, since some events are protected. Therefore, you can also inherit the LifeCycleListener class from Control, add it to the constructor on the page, listening and overriding all events.
If you only need the public “stages” of PreInit,Init,Load,DataBinding,PreRender,Unload,Disposed , take a look at the following approach (VB.Net, but I think you will understand this idea):
Public Enum LifeCyclePhase AfterPreInit AfterInit AfterLoad AfterDataBinding AfterPreRender AfterUnload AfterDisposed End Enum Public Interface ITrackingLifeCycle ReadOnly Property GetLifeCycleListener() As LifeCycleListener End Interface Public Class LifeCycleListener Public Sub New(ByVal ctrl As Control) Me._PageListening = ctrl.Page AddListener() End Sub Private _CurrentPhase As LifeCyclePhase Private _PageListening As Page Public ReadOnly Property CurrentPhase() As LifeCyclePhase Get Return _CurrentPhase End Get End Property Public ReadOnly Property PageListening() As Page Get Return _PageListening End Get End Property Private Sub AddListener() AddHandler _PageListening.PreInit, AddressOf PreInit AddHandler _PageListening.Init, AddressOf Init AddHandler _PageListening.Load, AddressOf Load AddHandler _PageListening.DataBinding, AddressOf DataBinding AddHandler _PageListening.PreRender, AddressOf PreRender AddHandler _PageListening.Unload, AddressOf Unload AddHandler _PageListening.Disposed, AddressOf Disposed End Sub Private Sub PreInit(ByVal sender As Object, ByVal e As EventArgs) Me._CurrentPhase = LifeCyclePhase.AfterPreInit End Sub Private Sub Init(ByVal sender As Object, ByVal e As EventArgs) Me._CurrentPhase = LifeCyclePhase.AfterInit End Sub Private Sub Load(ByVal sender As Object, ByVal e As EventArgs) Me._CurrentPhase = LifeCyclePhase.AfterLoad End Sub Private Sub DataBinding(ByVal sender As Object, ByVal e As EventArgs) Me._CurrentPhase = LifeCyclePhase.AfterDataBinding End Sub Private Sub PreRender(ByVal sender As Object, ByVal e As EventArgs) Me._CurrentPhase = LifeCyclePhase.AfterPreRender End Sub Private Sub Unload(ByVal sender As Object, ByVal e As EventArgs) Me._CurrentPhase = LifeCyclePhase.AfterUnload End Sub Private Sub Disposed(ByVal sender As Object, ByVal e As EventArgs) Me._CurrentPhase = LifeCyclePhase.AfterDisposed End Sub End Class
The handler in this class is called after the handler on the page itself, so if you fe check CurrentPhase in Page.Init, you will get PreInit. Therefore, I called this phase AfterPreInit.
Partial Public Class _Default Inherits System.Web.UI.Page Implements ITrackingLifeCycle Private lcl As New LifeCycleListener(Me) Public ReadOnly Property GetLifeCycleListener() As LifeCycleListener Implements ITrackingLifeCycle.GetLifeCycleListener Get Return lcl End Get End Property
Now you can check the life cycle phase everywhere, even without reference to the control via HttpContext.Current:
Public Class FooClass Public Shared Sub Foo() If Not (HttpContext.Current Is Nothing OrElse HttpContext.Current.Handler Is Nothing) Then If TypeOf HttpContext.Current.CurrentHandler Is ITrackingLifeCycle Then Dim page As ITrackingLifeCycle = DirectCast(HttpContext.Current.CurrentHandler, ITrackingLifeCycle) Dim phase As LifeCyclePhase = page.GetLifeCycleListener.CurrentPhase End If End If End Sub End Class
This is not tested enough and is not used independently, and, of course, it has improved, but perhaps it will help you in your current situation.