gpt4 book ai didi

asp.net - 在 ASP.NET WebForms 中找出控件处于生命周期的哪个阶段

转载 作者:行者123 更新时间:2023-12-04 09:56:21 26 4
gpt4 key购买 nike

从控件的外部,是否有可能找出页面生命周期的哪个阶段(初始化、加载、预渲染等),特定控件或页面正在执行?

例如,在伪代码中:

if myControl.CurrentLifeCycle == Lifecycle.Init
{ do something }

最佳答案

恐怕没有内置函数可以检查页面是什么页面生命周期阶段。
如果不处理 Page 本身的所有事件,也很难添加此功能,因为某些事件是 protected 。因此,您还可以从 Control 继承 LifeCycleListener 类,在构造函数中将其添加到页面监听并覆盖所有事件。

如果您只需要公共(public)“阶段”PreInit,Init,Load,DataBinding,PreRender,Unload,Disposed看看下面的方法(VB.Net,但我想你会明白的):

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

此类中的处理程序在页面本身的处理程序之后调用,所以如果你 f.e.检查 Page.Init 中的 CurrentPhase,您将获得 PreInit。因此,我将此阶段称为 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

您现在可以在任何地方检查生命周期阶段,即使没有通过 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

这既没有经过充分的测试,也没有被我自己使用,当然也可以改进,但它可能对你目前的情况有所帮助。

关于asp.net - 在 ASP.NET WebForms 中找出控件处于生命周期的哪个阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4094745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com