我在ASP.NET表单上有一个占位符,一个自定义类,它从多个源构建一个表,并在内部自动将类按钮添加到表中。
一切都很好,除了我的代码背后(数据绑定到FormView)我无法弄清楚如何连接我的事件。
在我的自定义类中,我有以下内容:
Dim Objective_addBtn As Button = New Button
Objective_addBtn.ID = 'I dynamically set the ID here
AddHandler Objective_addBtn.Click, AddressOf AddRow
在我的自定义课程中,我有以下内容:
Public Event AddNewRow As EventHandler
Private Sub AddRow(sender As Button, e As EventArgs)
Select Case True
Case sender.ID.Contains("ProblemBtn")
Case sender.ID.Contains("GoalBtn")
Case sender.ID.Contains("ObjectiveBtn")
RaiseEvent AddNewRow(Me, EventArgs.Empty)
Case sender.ID.Contains("TaskBtn")
Case Else
'Do Nothing - Should Not be here!
End Select
End Sub
我已经确认我正在使用RaiseEvent的代码块,但我无法弄清楚如何在我的代码中捕获该事件。
我目前的代码隐藏:
Partial Class <mypage>
Inherits System.Web.UI.Page
Private WithEvents CustomClass As New CustomClass
Protected Sub Page_Load(ByVal sender as object, e as EventArgs) Handled Me.Load
CustomClass = New CustomClass(Request.QueryString("pid"), Request.QueryString("sid"))
AddHandler CustomClass.AddNewRow, AddressOf NewRow
End Sub
End Class
最后,在后面的代码中,我有:
Protected Sub NewRow(ByVal sender As Object, e As EventArgs) Handles CustomClass.AddNewRow
MsgBox("NewRow Test")
End Sub
永远不会触发NewRow,MsgBox测试永远不会显示,并且逐步调试验证永远不会输入NewRow子。
答案 0 :(得分:1)
哈!得到它的工作:)这就是我做的:
首先,在我的Partial Class中,我引用了 -
Private WithEvents CustomClass as new CustomClass
Private WithEvents CustomClass as new CustomClass
然后我在Page_Init设置 -
CustomClass = New CustomClass(Request.QueryString("pid"), Request.QueryString("sid"))
AddHandler CustomClass.AddNewRow, AddressOf NewRow
显然你必须在Page_Init而不是Page_Load中设置它。现在正在执行NewRow()。希望这有助于那里的人!