0

I am dynamically adding a link button to every cell in a gridview. Adding the button works however the firing of the even handler doesn't. i need the linkbutton to call a function and pass some data for processing. My code is below. I have found solutions from the site that have gotten me this far. At the moment the gridview loads the cells with buttons in blue. when you click them they go back to plain text and no function is called.

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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
thomasfagin
  • 23
  • 1
  • 5

1 Answers1

0

Rather than adding Event ("add_datasource") to Linkbutton, trying using GridView's RowCommand Event. You will surely be able to fetch the event bubbled by linkbutton on Gridview's Row Command Event (along with the commandname and commandarguments)

Sankalp
  • 929
  • 1
  • 7
  • 16
  • Im not sure i understand. will this allow me to add alink button with unique params to each cell? – thomasfagin May 23 '12 at 14:58
  • Yes, It will. you will get unique value for each CommandArgument (that you specified unique for all linkbuttons) at Gridview_RowCommand event. – Sankalp May 25 '12 at 16:09