You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET中调用RaiseBubbleEvent后OnBubbleEvent未触发问题求助

问题原因及解决方案

核心问题是你误解了RaiseBubbleEvent的作用:这个方法的目的是将事件向上冒泡传递给当前控件的父控件,而不是触发自身的OnBubbleEvent方法。

在ASP.NET WebForm中,Page类的父控件通常为null,所以你在Page里调用RaiseBubbleEvent(Me, EventArgs.Empty)时,事件没有可传递的目标,自然不会触发你重写的OnBubbleEvent。

解决方法

方法1:直接调用自身的OnBubbleEvent

如果只是想执行OnBubbleEvent中的逻辑,不需要事件冒泡,直接在Refresh方法里调用该方法即可:

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")>
Private Sub Refresh(sender As Object, e As EventArgs)
    ' 直接调用自身的OnBubbleEvent方法
    OnBubbleEvent(Me, EventArgs.Empty)
End Sub

方法2:正确使用事件冒泡机制

如果你确实需要利用事件冒泡(比如子控件向页面传递事件),需要让子控件调用RaiseBubbleEvent,页面作为父控件的OnBubbleEvent才会被触发。

比如先定义一个自定义子控件:

Public Class CustomTriggerControl
    Inherits WebControl

    Public Sub TriggerBubble()
        ' 子控件调用RaiseBubbleEvent,事件会冒泡到父控件(页面)
        RaiseBubbleEvent(Me, EventArgs.Empty)
    End Sub
End Class

然后在页面中使用这个控件,并在Refresh里触发它的事件:

' 页面中声明自定义控件
Private myCustomControl As New CustomTriggerControl()

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")>
Private Sub Refresh(sender As Object, e As EventArgs)
    ' 触发子控件的事件冒泡
    myCustomControl.TriggerBubble()
End Sub

此时页面重写的OnBubbleEvent就会接收到子控件传递的事件,断点也会命中。

内容的提问来源于stack exchange,提问作者jp2code

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 02:48:15