页面事件如何从用户控件处理? (VB.net) [英] How can page events be handled from a usercontrol? (VB.net)
问题描述
Private Sub Page_PreLoad(ByVal sender As Object,ByVal e As System.EventArgs )Handle Page.PreLoad
但是,我知道得到编译错误:Handles子句需要一个WithEvents变量在包含类型或其基础类型之一中定义
由于Page属性继承自UserControl,我看不到如何轻松完成。
任何帮助感谢!
对于给出C#示例,我深表歉意,但我不记得如何在VB中
无论如何,在您的UserControl的Init中,您可以将您的Page_PreLoad方法显式分配为UserControl的页面属性的PreLoad事件的处理程序,而不是使用句柄方法声明中的语法。您的示例是尝试为UserControl对象的事件分配事件处理程序,以便UserControl对象不引发的事件。如你所说,UserControl不会继承自Page,这是PreLoad事件所在的地方。但是,UserControl将包含一个Page对象作为其属性之一,这又将PreLoad暴露为可以为其分配处理程序的事件。无论如何,这编译并且接近你正在寻找的东西(使用C风格的评论来保存WMD语法高亮)。
受保护的覆盖Sub OnInit(ByVal e As System.EventArgs)
//将P_PLLAD作为事件处理程序
//分配给Control的页面属性
AddHandler Me.Page的PreLoad事件。 PreLoad,AddressOf Page_PreLoad
MyBase.OnInit(e)
End Sub
Private Sub Page_PreLoad(ByVal sender As Object,ByVal e As System.EventArgs)
//在这里做某事
End Sub
我不知道这是否符合您的目的 - 正如Stephen Wrighton上面指出的那样,在页面生命周期中可能会有一个更好的方法与其中一个不同的事件。然而,建立在他所说的内容上,这应该是有效的,因为控件的OnInit被调用进行事件处理程序分配,然后引发页面的OnLoad事件,然后执行控件中的事件处理程序。
I would like some code to execute at the "preload" stage of my usercontrol's lifecycle. However the preload event is only available on the Page object. So, I added the following method to my usercontrol:
Private Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Page.PreLoad
However, I know get the compile error: "Handles clause requires a WithEvents variable defined in the containing type or one of its base types"
As the Page property is inherited from UserControl I don't see how this can easily be done.
Any help gratefully received!
I apologize for giving a C# example but I can't remember how to do it in VB...
Anyway, in your UserControl's Init you could explicitly assign your Page_PreLoad method as the handler for the PreLoad event of the UserControl's Page property instead of using the "Handles" syntax in the method declaration. What your example is doing is trying to assign an event handler to an event on the UserControl object for an event that the UserControl object doesn't raise. As you noted, UserControl doesn't inherit from Page, which is where the PreLoad event lives. UserControl does, however contain a Page object as one of its properties, which in turn exposes PreLoad as an event to which you can assign a handler. Anyway, this compiles and approaches what it sounds like you are looking for (using C-style comments to preserve WMD syntax highlighting).
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.
这篇关于页面事件如何从用户控件处理? (VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!