0

我已经实现了以下类来动态创建gridview。我面临处理 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

请建议我哪里出错了,或者让我知道更好的方法来实现这一点。

4

0 回答 0