Vb.net 从类调用事件处理程序引用
我已经实现了以下类来动态创建gridview。我面临处理imagebutton事件的问题。我想在我的页面而不是类中处理事件。但是下面的代码显示了错误Vb.net 从类调用事件处理程序引用,vb.net,events,event-handling,dynamic-controls,Vb.net,Events,Event Handling,Dynamic Controls,我已经实现了以下类来动态创建gridview。我面临处理imagebutton事件的问题。我想在我的页面而不是类中处理事件。但是下面的代码显示了错误 Public Class MyTemplateField Implements System.Web.UI.ITemplate Private listItemType As ListItemType Private id As String = String.Empty Private bind As String =
Public Class MyTemplateField
Implements System.Web.UI.ITemplate
Private listItemType As ListItemType
Private id As String = String.Empty
Private bind As String = String.Empty
Private ctltype As String = String.Empty
Private evnt As EventHandler
Sub New(ByVal listItemType As ListItemType, ByVal bind As String, ByVal id As String, ByVal ctltype As String, Optional ByVal evnt As eventhandler = test_click)
Me.listItemType = listItemType
Me.id = id
Me.bind = bind
Me.ctltype = ctltype
Me.evnt = evnt
End Sub
Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn
If listItemType = listItemType.Item Then
If ctltype = "Button" Then
Dim imgbtn As New ImageButton
imgbtn.ID = "imgbtn" & id
imgbtn.ImageUrl = "~/images/clk.png"
AddHandler imgbtn.Click, AddressOf evnt
container.Controls.Add(imgbtn)
Else
Dim lbl As New Label
lbl.ID = "lbl" & id
AddHandler lbl.DataBinding, AddressOf lbl_DataBinding
container.Controls.Add(lbl)
End If
ElseIf listItemType = listItemType.EditItem Then
.......
End If
End Sub
Private Sub lbl_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim lbl As Label = DirectCast((sender), Label)
Dim row As GridViewRow = DirectCast((lbl.NamingContainer), GridViewRow)
lbl.Text = DataBinder.Eval(row.DataItem, bind).ToString()
End Sub
.........
End Class
第页中的代码
tfld = New TemplateField
tfld.ItemTemplate = New MyTemplateField(ListItemType.Item, "Order", "Nest", "Button", imgbtn_click)
Public Sub imgbtn_click(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
请告诉我哪里出了问题,或者让我知道更好的实现方法。不知道为什么我们要猜测错误消息。猜测:AddressOf需要一个方法,不能使用委托。因此,请编写一个只调用“evnt”的小助手方法。@HansPassant您可以分享一个如何实现它的示例吗?Wny?你已经为lbl_数据绑定做了,再做一次。