0

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
2

2 Answers 2

1

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
1
  • I'd recommend not to use IsNothing which is a legacy-support function. Use the Is or IsNot operator, e.g. If fileUpload.PostedFile IsNot Nothing .... Also, you're doing a TryCast and then not confirming that it succeeded; if you're going to plow ahead, do a CType so that it will throw then with a type conversion exception if it fails, rather than throwing later with a more obscure null reference exception.
    – Craig
    Commented Apr 17, 2020 at 13:41
0

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!

2
  • The Button is obviously being displayed because it's being clicked. The code adds the Button to a cell, which is added to a row, which is added to a table, which is presumably already on the page. I don't think that displaying the Button is the issue. Commented Apr 17, 2020 at 3:19
  • Foiled again. I'll leave this so nobody writes it again.
    – laancelot
    Commented Apr 17, 2020 at 3:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.