Event does not fire when wired-in dynamically using Addhandler

Last post 08-20-2007 7:50 AM by MAB. 2 replies.

Sort Posts:

  • Event does not fire when wired-in dynamically using Addhandler

    08-16-2007, 11:17 AM
    • Loading...
    • MAB
    • Joined on 01-13-2006, 4:09 AM
    • Posts 27

    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 control

    Dim 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 Sub

    Shared Sub Faculty_Click(ByVal sender As Object, ByVal e As EventArgs)

    MsgBox("Clicked: " & CType(sender, LinkButton).Text)

    End Sub

     

    Public Class GridViewTemplate

    Implements ITemplate

    Private templateType As DataControlRowType

    Private columnName As String

    Sub New(ByVal type As DataControlRowType, ByVal colname As String)

    templateType = type

    columnName = colname

    End Sub

    Sub InstantiateIn(ByVal container As System.Web.UI.Control) _

    Implements ITemplate.InstantiateIn

    ' Create the content for the different row types.

    Select Case templateType

    Case DataControlRowType.Header

    Dim 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 LinkButton

    AddHandler Faculty.DataBinding, AddressOf Faculty_DataBindingAddHandler Faculty.Click, AddressOf Faculty_Click

    container.Controls.Add(Faculty)

    Case Else

    ' Insert code to handle unexpected values.

    End Select

    End 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 Sub

    End Class

    End Class

  • Re: Event does not fire when wired-in dynamically using Addhandler

    08-16-2007, 12:33 PM
    Answer
    • Loading...
    • joteke
    • Joined on 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,674
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Move the column instantation to Page_Init

     

       Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim customField As New TemplateField
            customField.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "Faculty")
            customField.HeaderTemplate = New GridViewTemplate(DataControlRowType.Header, "Faculty")
            AuthorsGridView.Columns.Add(customField)
        End Sub
     
    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: Event does not fire when wired-in dynamically using Addhandler

    08-20-2007, 7:50 AM
    • Loading...
    • MAB
    • Joined on 01-13-2006, 4:09 AM
    • Posts 27

    Thank you. That's fine now.

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter