如何从用户控件处理页面事件?(VB.net)

How can page events be handled from a usercontrol? (VB.net)(如何从用户控件处理页面事件?(VB.net))
本文介绍了如何从用户控件处理页面事件?(VB.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的用户控件生命周期的预加载"阶段执行一些代码.但是 preload 事件仅在 Page 对象上可用.所以,我在我的用户控件中添加了以下方法:

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 

但是,我知道会出现编译错误:Handles 子句需要在包含类型或其基类型之一中定义的 WithEvents 变量"

However, I know get the compile error: "Handles clause requires a WithEvents variable defined in the containing type or one of its base types"

由于 Page 属性是从 UserControl 继承的,所以我看不出这是如何轻松完成的.

As the Page property is inherited from UserControl I don't see how this can easily be done.

感谢您的任何帮助!

推荐答案

我很抱歉给出了一个 C# 示例,但我不记得如何在 VB 中执行...

无论如何,在 UserControl 的 Init 中,您可以将 Page_PreLoad 方法显式指定为 UserControl 的 Page 属性的 PreLoad 事件的处理程序,而不是在方法声明中使用Handles"语法.您的示例正在尝试将事件处理程序分配给 UserControl 对象上的事件,以处理 UserControl 对象未引发的事件.正如您所指出的,UserControl 不继承自 Page,这是 PreLoad 事件所在的位置.然而,UserControl 确实包含一个 Page 对象作为其属性之一,这反过来又将 PreLoad 作为一个事件公开,您可以为其分配一个处理程序.无论如何,这会编译并接近您正在寻找的内容(使用 C 风格的注释来保留 WMD 语法突出显示).

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

我不确定这是否符合您的目的 - 正如 Stephen Wrighton 上面指出的那样,对于页面生命周期中的不同事件之一,可能有更好的方法.但是,根据他所说的,这应该可以工作,因为调用控件的 OnInit 进行事件处理程序分配,然后引发 Page 的 OnLoad 事件,然后执行控件内的事件处理程序.

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)