我很抱歉给出了一个 C# 示例,但我不记得如何在 VB 中做到这一点......
无论如何,在您的 UserControl 的 Init 中,您可以显式地将 Page_PreLoad 方法指定为 UserControl 的 Page 属性的 PreLoad 事件的处理程序,而不是在方法声明中使用“Handles”语法。您的示例正在尝试将事件处理程序分配给 UserControl 对象上的事件,以处理 UserControl 对象未引发的事件。正如您所指出的,UserControl 不继承自 Page,这是 PreLoad 事件所在的位置。然而,UserControl 确实包含一个 Page 对象作为其属性之一,这反过来又将 PreLoad 作为一个事件公开,您可以为其分配一个处理程序。无论如何,这会编译并接近您正在寻找的内容(使用 C 风格的注释来保留 WMD 语法突出显示)。
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
// this assigns Page_PreLoad as the event handler
// for the PreLoad event of the Control's Page property
AddHandler Me.Page.PreLoad, AddressOf Page_PreLoad
MyBase.OnInit(e)
End Sub
Private Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs)
// do something here
End Sub
I'm not sure if that serves your purpose--as Stephen Wrighton indicated above, there may be a better way with one of the different events in the page lifecycle. However, building on what he said, this should work because the control's OnInit is called in which the event handler assignment is made, then the Page's OnLoad event is raised and then the event handler within the control is executed.