在VB.NET中,如何监听应用程序中任何下拉列表的加载事件?
问题描述:
我有几个下拉列表在应用程序的不同页面。在VB.NET中,如何监听应用程序中任何下拉列表的加载事件?
我需要运行一些功能,当在全局应用程序中加载任何下拉菜单时。
我尝试以下,但不工作:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetAllDropdowns(Me.Page)
End Sub
Private Sub GetAllDropdowns(ByVal Ctrl As Control)
Dim dd As DropDownList = Nothing
For Each c In Ctrl.Controls
If TypeOf c Is DropDownList Then
dd = DirectCast(c, DropDownList)
AddHandler dd.Load, AddressOf doIt
Else
GetAllDropdowns(c)
End If
Next
End Sub
Private Sub doIt(ByVal sender As Object, ByVal e As EventArgs)
'Do whatever....
End Sub
答
我觉得现在的问题是,你正在使用的事件。
Page_Load事件在控件的所有加载事件之后运行,因此sub doIT不会运行。
尝试在Page_Init事件中移动您的代码,这似乎更适合于此任务。
有关asp.net页面生命周期的更多信息,请参阅here。
也许提供一些代码?这个问题无法回答,因为它太宽泛了。 –
plz检查更新的帖子 – Mohamad