加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

找出ASP.NET WebForms中控件的生命周期的哪个阶段

发布时间:2020-12-16 04:02:10 所属栏目:asp.Net 来源:网络整理
导读:从控件的外部,是否可以找出Page LifeCycle(Init,Load,PreRender等)的哪个阶段,特定控件或页面是什么? 例如,在伪代码中: if myControl.CurrentLifeCycle == Lifecycle.Init{ do something } 解决方法 我担心没有内置函数可以检查页面的页面生命周期阶段. 如
从控件的外部,是否可以找出Page LifeCycle(Init,Load,PreRender等)的哪个阶段,特定控件或页面是什么?

例如,在伪代码中:

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

解决方法

我担心没有内置函数可以检查页面的页面生命周期阶段.
如果不处理页面本身中的所有事件,也很难添加此功能,因为某些事件受到保护.因此,您还可以从Control继承LifeCycleListener类,将其在构造函??数中添加到侦听页面并覆盖所有事件.

如果你只需要公共“阶段”PreInit,Init,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中的CurrentPhase.Init你会得到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

这既没有经过充分的测试,也没有被我自己使用过,而且确实可以改进,但也许它可以帮助你解决当前的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读