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

前端之家收集整理的这篇文章主要介绍了找出ASP.NET WebForms中控件的生命周期的哪个阶段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从控件的外部,是否可以找出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

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

原文链接:https://www.f2er.com/aspnet/246992.html

猜你在找的asp.Net相关文章