我动态创建的ImageButtons不会启动click事件

时间:2016-04-08 14:00:13

标签: asp.net vb.net imagebutton addhandler

我需要创建一个表,其第一列是一个图像按钮,在单击时删除整行(实际上它会删除数据库行,然后重新加载HTML表)。

表和按钮按预期显示,但是当我单击按钮时,没有任何反应。

为了使每个按钮都可以点击,我使用了AddHandler

删除是处理点击的子,它将获取按钮的ID(其中包含用户想要删除的行的数据库表ID)然后调用存储过程将ID作为参数,最后重新加载HTML表。我还没有实现它,但是调试器无法实现它。

VB代码如下

    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

出于某种原因,单击按钮时会触发Page_Load DropDownList。但是我使用Page_Load获得了相同的行为,第一个PickProject_SelectedIndexChanged被激活,然后{{1}},事件处理程序按预期工作。

我用Google搜索并搜索了StackOverflow,但是没有一个解决方案似乎有效。

我错过了什么?

0 个答案:

没有答案