Pradip Kr. Shukla
Pradip Kr. Shukla

Reputation: 43

Click event is not firing of dynamically added link button in grid view

I am writing the following code on the rowdatabound of the grid view and i am not getting the click event of link button

Protected Sub CoolGRDSourcedDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles CoolGRDSourcedDetails.RowDataBound
        Dim iLoop As Integer
        Dim lbtnCountDetails As New LinkButton
        e.Row.Cells.RemoveAt(1)
        If e.Row.RowType = DataControlRowType.Header Then
            For iLoop = 1 To (dscolumns / 2) - 1
                e.Row.Cells(iLoop).Attributes.Add("colspan", "2")
                If iLoop = 1 Then
                    e.Row.Cells(iLoop).Text = "Self"
                Else
                    e.Row.Cells(iLoop).Text = "Child" & iLoop - 2
                End If
            Next
            e.Row.Cells(iLoop).Text = "Total"
        ElseIf e.Row.RowType = DataControlRowType.DataRow Then
            For iLoop = 1 To dscolumns - 2
                If iLoop Mod 2 <> 0 Then
                    e.Row.Cells(iLoop + 1).Text = Format(IIf(CInt(e.Row.Cells(iLoop).Text) <> 0, (CInt(e.Row.Cells(iLoop).Text) / value) * 100, 0), "0.00") & "%"
                    If CInt(e.Row.Cells(iLoop).Text) <> 0 Then
                        e.Row.Cells(iLoop).Controls.Add(lbtnCountDetails)
                        lbtnCountDetails.Text = e.Row.Cells(iLoop).Text
                        lbtnCountDetails.CommandArgument = "strstatus"
                        lbtnCountDetails.Attributes.Add("OnClick", "lbtnCountDetails_Click")
                    End If
                End If
            Next
        End If
    End Sub

'Click event is here

Protected Sub lbtnCountDetails_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim strStatus As String = CType(sender, LinkButton).CommandArgument
    End Sub

Upvotes: 1

Views: 3788

Answers (4)

Azhar Khorasany
Azhar Khorasany

Reputation: 2709

Try this line:

 Dim WithEvents lbtnCountDetails As New LinkButton

Upvotes: 0

Gopal Krishna Ranjan
Gopal Krishna Ranjan

Reputation: 878

Go to the below line in your code and make changes after that: -

ElseIf e.Row.RowType = DataControlRowType.DataRow Then

Actually you forgot to add an event handler to the event of LinkButton1, so you are not able to get the click event of the LinkButton.

The changes you have to make : -

If CInt(e.Row.Cells(iLoop).Text) <> 0 Then
                            LinkButton1.Text = e.Row.Cells(iLoop).Text
                            AddHandler LinkButton1.Click, AddressOf Me.LinkButton1_Click
                            LinkButton1.CommandArgument = e.Row.Cells(0).Text
                            e.Row.Cells(iLoop).Controls.Add(LinkButton1)
                        End If

Try it.

Upvotes: 1

Bibhu
Bibhu

Reputation: 4081

Give a command name to your link button, like this

CommandName="Show" 

and in the code behind, handle it as,

    protected void gridviewReport_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Show")
        {
            // this will return the row index
            int index = Convert.ToInt32(e.CommandArgument);
            // your code goes here
        }
    }

Upvotes: 0

lnu
lnu

Reputation: 1414

You can't add the event binding with an Attributes.Add. You can use the RowCommand event from the gridview. This link has a good example of using RowCommand. Same question here.

Upvotes: 0

Related Questions