将链接按钮动态添加到gridview。不开火。

时间:2012-05-08 02:40:33

标签: asp.net vb.net gridview postback linkbutton

我正在为gridview中的每个单元格动态添加一个链接按钮。添加按钮有效但偶数处理程序的触发不起作用。我需要linkbutton来调用一个函数并传递一些数据进行处理。我的代码如下。我从网站上找到了解决这个问题的解决方案。 目前,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

1 个答案:

答案 0 :(得分:0)

不是将事件(“add_datasource”)添加到Linkbutton,而是尝试使用GridView的RowCommand事件。您肯定能够通过Gridview的行命令事件(以及命令名和命令参数)上的linkbutton获取冒泡的事件