kdubbers
kdubbers

Reputation: 43

Dynamically Added Event Handler Not Firing in VB.NET

For some reason when I click the "Remove" button nothing happens. I'm assuming that this has something to do with view state and losing the handlers on page load, though I still can't make sense of it. I'm recreating the handler every time the page loads, why isn't this working?

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    If IsPostBack Then
        If Not IsNothing(fileUpload.PostedFile) AndAlso fileUpload.PostedFile.ContentLength > 0 Then
            _files = TryCast(Session("FilesToSend"), List(Of document))
            _files.Add(New Document(fileUpload.FileName, fileUpload.FileBytes, fileUpload.FileBytes.Length))
            loadTable()
        End If
    Else
        Session("FilesToSend") = New List(Of document)
    End If

End Sub

Private Sub loadTable()

    Dim count As Integer = 0
    For Each doc In _files
        Dim r As New TableRow()

        'Add Filename Cell
        Dim filenameCell As New TableCell()
        filenameCell.Text = doc.filename
        r.Cells.Add(filenameCell)

        'Add Size Cell
        Dim sizeCell As New TableCell()
        sizeCell.Text = doc.fileSize
        r.Cells.Add(filenameCell)

        'Add Remove Button Cell
        Dim deleteButton As New Button
        Dim deleteCell As New TableCell()
        With deleteButton
            .Text = "Remove"
            '.ID = "deleteButton" + count.ToString()
            deleteCell.Controls.Add(deleteButton)
            AddHandler deleteButton.Click, AddressOf deleteRow_Click
        End With
        r.Cells.Add(deleteCell)
        'AddHandler deleteButton.Click, New EventHandler(AddressOf deleteRow_Click)

        'Add Row to Table
        uploadedDocumentsTable.Rows.Add(r)

        count += 1
    Next
End Sub

Protected Sub deleteRow_Click(sender As Object, e As System.EventArgs)
    _files = TryCast(Session("FilesToSend"), List(Of Document))
    Dim deleteButton = TryCast(sender, Button)
    _files.RemoveAt(sender.ID)
    loadTable()
End Sub

Upvotes: 0

Views: 377

Answers (2)

kdubbers
kdubbers

Reputation: 43

Well after some more trial and error I finally figured it out. So the first issue was that I needed to add ".CausesValidation = False". This triggered the Postback, but then I needed to do some rearranging to make sure the controls were still getting loaded. Here's what worked:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    If IsPostBack Then
        _files = TryCast(Session("FilesToSend"), List(Of Document))
        If Not IsNothing(fileUpload.PostedFile) AndAlso fileUpload.PostedFile.ContentLength > 0 Then
            _files.Add(New Document(fileUpload.FileName, fileUpload.FileBytes, fileUpload.FileBytes.Length))
        End If
    Else
        Session("FilesToSend") = New List(Of document)
    End If
    loadTable()

End Sub

Private Sub loadTable()
    uploadedDocumentsTable.Rows.Clear()
    Dim count As Integer = 0
    For Each doc In _files
        Dim r As New TableRow()

        'Add Filename Cell
        Dim filenameCell As New TableCell()
        filenameCell.Text = doc.filename
        r.Cells.Add(filenameCell)

        'Add Size Cell
        Dim sizeCell As New TableCell()
        sizeCell.Text = doc.fileSize
        r.Cells.Add(filenameCell)

        'Add Remove Button Cell
        Dim deleteButton As New Button
        Dim deleteCell As New TableCell()
        With deleteButton
            .Text = "Remove"
            .CausesValidation = False
            .ID = count.ToString()
            deleteCell.Controls.Add(deleteButton)
            AddHandler deleteButton.Click, AddressOf deleteRow_Click
        End With
        r.Cells.Add(deleteCell)

        'Add Row to Table
        uploadedDocumentsTable.Rows.Add(r)

        count += 1
    Next
End Sub

Protected Sub deleteRow_Click(sender As Object, e As System.EventArgs)
    _files.RemoveAt(sender.ID)
    loadTable()
End Sub

Upvotes: 1

laancelot
laancelot

Reputation: 3207

You forgot to add the button to the Form. Modify it this way:

'Add Remove Button Cell
    Dim deleteButton As New Button
    Dim deleteCell As New TableCell()
    With deleteButton
        .Text = "Remove"
        '.ID = "deleteButton" + count.ToString()
        deleteCell.Controls.Add(deleteButton)
        AddHandler deleteButton.Click, AddressOf deleteRow_Click
    End With
    Me.Controls.Add(deleteButton)

Surprise, you button is now located at position (0, 0), and the event is now listened to. You may want to modify it's position too, I guess.

Have fun!

Upvotes: 0

Related Questions