根据我的理解,将控件添加到页面的最佳时间是在OnInit事件中。但是如果你通过另一个控件事件添加一个控件,你就没有选择它会在加载事件之后添加事件。
例如:
Private Sub catalog_Init(sender As Object, e As System.EventArgs) Handles Me.Init
p1.Controls.Add(New blah)
End Sub
Public Class blah
Inherits Control
Protected Overrides Sub OnInit(e As EventArgs)
Debug.WriteLine("- control oninit")
MyBase.OnInit(e)
Dim b As Button = New Button
b.Text = "link to me"
AddHandler b.Click, AddressOf blah2
Me.Controls.Add(b)
End Sub
Sub blah2(sender As Object, e As EventArgs)
Debug.WriteLine("* button event fires")
Dim b As Button = New Button
b.Text = "another button!"
AddHandler b.Click, AddressOf blah2
Me.Controls.Add(b)
End Sub
End Class
如何触发第二个按钮的事件?