グリッドビューを動的に作成するために、次のクラスを実装しました。imagebutton イベントの処理に問題があります。クラスではなく自分のページでイベントを処理したい。しかし、以下のコードはエラーを示しています。
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
どこが間違っているかを提案するか、これを実装するためのより良い方法を教えてください。