Find out what stage of lifecycle control is in ASP.NET WebForms

From the outside of the control, you can find out at what stage of the page life cycle (Init, Load, PreRender, etc.), which specific control or page is up to?

For example, in pseudocode:

if myControl.CurrentLifeCycle == Lifecycle.Init { do something } 
+6
source share
5 answers

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.

+6
source share

I think what you are trying to achieve is conceptually wrong because you think of page events as page state. It is not possible to go to the "OnInit / OnLoad / ..." page because it is an event.

Why do you need this? perhaps we could offer you a better approach to achieve your goal.

+3
source share

“Out of your control” is “inside” another control or page, or something in a related state of the life cycle. You already know his condition without testing.

+3
source share

As far as I understand the page life cycle, you cannot do this. Basically, a page class is someone who needs to raise events in a specific order. There is nothing in the construction that will show the processing stage. But for something, you can create a property and configure this property at different stages of processing.

+2
source share

I found that there actually exists an internal property of the Page class that does exactly what I'm looking for: this is an enumeration called ControlState:

 internal enum ControlState { Constructed, FrameworkInitialized, ChildrenInitialized, Initialized, ViewStateLoaded, Loaded, PreRendered } 

I believe that you can access internal members in C # 4, see here

+2
source share

All Articles