如何页面事件从一个用户控件处理? (VB.net) [英] How can page events be handled from a usercontrol? (VB.net)

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

问题描述

我想约code。在我的用户生命周期的preLOAD的阶段来执行。然而,preLOAD事件仅在页面对象上。所以,我添加了下面的方法来我的用户:

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"

作为页面属性从用户控件继承我看不出如何能很容易地完成。

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

任何帮助感激地接受!

Any help gratefully received!

推荐答案

<罢>我给一个C#示例道歉,但我不记得如何做到这一点在VB ...

不管怎样,在你的用户控件的init你可以明确地分配你的Page_ preLOAD方法的处理程序中用户控件的页面属性,而不是在方法声明中使用的把手语法的preLOAD事件。你的例子做的是试图将事件处理程序分配给用户控件对象上的事件一个事件,UserControl对象不提高。正如你提到的,用户控件没有从页面继承,这哪里是preLOAD事件的生活。用户控件确实,但是包含一个Page对象及其属性,而这又暴露了preLOAD作为一个事件,你可以指定一个处理程序中的一个。不管怎么说,这将编译和办法,这听起来像你正在寻找(使用C风格的注释,以preserve大规模杀伤性武器的语法高亮)。

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

我不知道,如果是服务于您的目的 - 斯蒂芬Wrighton上文所述,则可能是在页面生命周期的不同事件的一个更好的方法。然而,建立对他说什么,这应该工作,因为该控件的OnInit中被称为该事件处理程序分配由,那么页面的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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆