在ASP.NET Web应用程序中,类的事件根本不会触发。
它不会产生任何错误,但也不起作用。
到目前为止我尝试过:
这是最简单的例子:
testEvent.aspx:
Public Class testEvent
Inherits System.Web.UI.Page
Public WithEvents TestClass As MyClassWithEvents
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TestClass = New MyClassWithEvents
End Sub
Private Sub TestClass_SimpleEvent(txt As String) Handles TestClass.SimpleEvent
Response.Write("Event fired from class: " + txt + "<br />")
End Sub
End Class
MyClassWithEvents.vb:
Public Class MyClassWithEvents
Public Event SimpleEvent(txt As String)
Public Sub New()
RaiseEvent SimpleEvent("Sub New() in Class")
SomeMethod()
End Sub
Public Sub SomeMethod()
RaiseEvent SimpleEvent("SomeMethod() called in Class")
End Sub
End Class
答案 0 :(得分:0)
当我在SomeMethod()
事件中致电PreRender
时:
Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
Me.TestClass.SomeMethod()
End Sub
工作正常。对于要触发的事件,Load
的生命周期中必须过早。