0

我正在为gridview中的每个单元格动态添加一个链接按钮。添加按钮有效,但偶数处理程序的触发无效。我需要链接按钮来调用函数并传递一些数据进行处理。我的代码如下。我从网站上找到了让我走到这一步的解决方案。目前,gridview 加载带有蓝色按钮的单元格。当您单击它们时,它们会返回纯文本并且不会调用任何函数。

Private Sub gv_datasource_options_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_datasource_options.RowDataBound
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then
            For Each c As TableCell In e.Row.Cells
                If Len(c.Text) > 0 And c.Text <> "&nbsp;" Then
                    Dim v_lb As New LinkButton()
                    v_lb.Text = c.Text                        
                    AddHandler v_lb.Click, AddressOf add_datasource
                    v_lb.Attributes.Add("AutoPostback", "True")
                    v_lb.Attributes.Add("runat", "Server")
                    v_lb.Attributes.Add("AutoEventWireup", "True")
                    v_lb.CommandName = "NumClick"
                    v_lb.CommandArgument = e.Row.Cells(0).ToString & "|" & gv_datasource_options.HeaderRow.Cells(e.Row.Cells.GetCellIndex(c)).Text
                    c.Controls.Add(v_lb)
                    Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
                    sm.RegisterAsyncPostBackControl(v_lb)
                End If
            Next
        End If
    Catch ex As Exception
        cl_Error.cl_Erorr.LogError(ex, txt_userid.Value, ex.ToString)
    End Try
End Sub
Private Sub add_datasource(sender As Object, e As CommandEventArgs)
    Try
        hf_datasource_id.Value = Left(e.CommandArgument.ToString(), (Len(e.CommandArgument.ToString) - InStr(e.CommandArgument.ToString, "|")))
        hf_datasource_column.Value = Left(e.CommandArgument.ToString(), (Len(e.CommandArgument.ToString) - InStr(e.CommandArgument.ToString, "|")))
        hf_datasource_tableid.Value = tv_content.SelectedValue
        p_datasource.Visible = False
    Catch ex As Exception
        cl_Error.cl_Erorr.LogError(ex, txt_userid.Value, ex.ToString)
    End Try

End Sub
4

1 回答 1

0

尝试使用 GridView 的 RowCommand 事件,而不是将 Event ("add_datasource") 添加到 Linkbutton。您肯定能够获取 Gridview 的行命令事件上的链接按钮冒泡的事件(以及命令名和命令参数)

于 2012-05-09T13:49:05.630 回答