0

I need to create a table whose first column is an image button that deletes the whole row when clicked (actually it deletes a database row then reloads the HTML table).

The table and the buttons display as expected, but when I click a button, nothing happens.

To make each button clickable, I used AddHandler.

deletion is the sub that handles the clicks, it will get the button's ID (which contains the database table id for the row the user wants to delete) then calls a stored procedure with the ID as parameter and finally reloads the HTML table. I didn't implement it yet but the debugger can't reach it.

The VB code is as follows

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Request.Cookies("myuser") Is Nothing OrElse Request.Cookies("myuser")("isconnected") <> "1" Then
        Response.Redirect("/Default.aspx")
    Else
        N_User = CInt(Request.Cookies("myuser")("N_User"))
        Dim dbc As DBConnection 
        dbc = New DBConnection("192.168.1.45", "CorpDB", "someuser", "xxxxxxx")
        Dim dt As DataTable
        Try
            dt = dbc.getQueryData("select lastname, isnull(firstname,'') from itc where N_User=" & CStr(N_User))
            If dt.Rows.Count > 0 Then
            End If
        Catch ex As Exception
            ' do nothing for now
        End Try
    End If
    FillTable()
End Sub


Protected Sub deletion(sender As Object, e As ImageClickEventArgs)
    ' confirm deletion
    ' Run a stored procedure to delete the comment row
    FillTable()
End Sub

Private Sub FillTable()
    Dim dbc As DBConnection
    Dim dt As DataTable
    Dim tr As TableRow
    Dim tc As TableCell
    Dim ibutton As ImageButton
    Dim sqlquery As String
    If PickProject.SelectedValue = "" Then
        Exit Sub
    End If
    sqlquery = "select N_follow, Name=isnull(isnull(lastname + ' ','') + Nom, 'Anon'), Date=convert(varchar,DFollow,103), Stage=isnull(FreeField1,''), [Text]=isnull(dbo.RTFtoText(txt),'') from AF_FOLLOW a left outer join USERS u on u.N_User = a.N_User_Create where Numero=" & PickProject.SelectedValue & "order by DFollow desc"
    dbc = New DBConnection("192.168.1.45", "CorpDB", "someuser", "xxxxxxx")
    Try
        dt = dbc.getQueryData(sqlquery)
    Catch ex As Exception
        ' Show something,
        Exit Sub
    End Try

    If dt.Rows.Count > 0 Then
        CommentTable.Rows.Clear()
        For Each row As DataRow In dt.Rows
            ' Create a new row
            tr = New TableRow

            ' column #1 : delete button
            tc = New TableCell
            tc.CssClass = "SelectDel"
            ibutton = New ImageButton()

            ibutton.ID = "ibutton_" & row.Item("N_follow")
            ibutton.ImageUrl = "img/remove_icon.png"
            AddHandler ibutton.Click, AddressOf deletion
            tc.Controls.Add(ibutton)

            tr.Cells.Add(tc)

            ' Column #2 : Date
            tc = New TableCell
            tc.Text = row.Item("Date")
            tr.Cells.Add(tc)

            ' Column #3 : Stage
            tc = New TableCell
            tc.Text = row.Item("Stage")
            tr.Cells.Add(tc)

            ' Column #4 : name
            tc = New TableCell
            tc.Text = row.Item("Name")
            tr.Cells.Add(tc)

            ' Column #5 : Comment
            tc = New TableCell
            tc.Text = row.Item("Text")
            tr.Cells.Add(tc)

            ' Add the created row to the table
            CommentTable.Rows.Add(tr)
        Next
        CommentTable.Visible = True
    Else
    End If
End Sub

For some reason, it's Page_Load that gets triggered when I click a button. But I got the same behavior with a DropDownList, first Page_Load gets fired up, then PickProject_SelectedIndexChanged and the event handler works as intended.

I googled and searched StackOverflow a lot , but none of the solutions suggested seems to work.

What did I miss ?

CubeJockey
  • 2,209
  • 8
  • 24
  • 31

0 Answers0