Hi,
I have based my code on the Microsoft example, adding a linkbutton dynamically to a datagrid. The linkbutton has a OnClick event that I have wired-in dynamically at run-time. The trouble is that when clicked, the event handler does not fire. Can anyone explain why, please?
My code is:
Partial Class _DefaultInherits System.Web.UI.Page
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
' Add a template field consisting of a single linkbutton controlDim customField As New TemplateField
customField.ItemTemplate =
New GridViewTemplate(DataControlRowType.DataRow, "Faculty")customField.HeaderTemplate = New GridViewTemplate(DataControlRowType.Header, "Faculty")
AuthorsGridView.Columns.Add(customField)
End If
End SubShared Sub Faculty_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox(
"Clicked: " & CType(sender, LinkButton).Text)End Sub
Public Class GridViewTemplate
Implements ITemplatePrivate templateType As DataControlRowType
Private columnName As StringSub New(ByVal type As DataControlRowType, ByVal colname As String)
templateType = type
columnName = colname
End SubSub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements ITemplate.InstantiateIn
' Create the content for the different row types.Select Case templateType
Case DataControlRowType.HeaderDim lc As New Literal
lc.Text =
"<b>" & columnName & "</b>"
' Add the controls to the Controls collection
' of the container.
container.Controls.Add(lc)
Case DataControlRowType.DataRow
Dim Faculty As New LinkButtonAddHandler Faculty.DataBinding, AddressOf Faculty_DataBindingAddHandler Faculty.Click, AddressOf Faculty_Click
container.Controls.Add(Faculty)
Case Else
' Insert code to handle unexpected values.
End SelectEnd Sub
Private Sub Faculty_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim l As LinkButton = CType(sender, LinkButton)Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)
l.Text = DataBinder.Eval(row.DataItem,
"Faculty").ToString()
End SubEnd Class
End
Class