Click事件未在网格视图中触发动态添加的链接按钮

时间:2011-12-16 07:30:28

标签: asp.net vb.net

我在网格视图的rowdatabound上编写以下代码,我没有得到链接按钮的click事件

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

'点击事件在这里

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

4 个答案:

答案 0 :(得分:1)

转到代码中的以下行并在此之后进行更改: -

ElseIf e.Row.RowType = DataControlRowType.DataRow Then

实际上你忘了在LinkBut​​ton1的事件中添加一个事件处理程序,所以你无法获得LinkBut​​ton的click事件。

您必须做出的更改: -

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

试试吧。

答案 1 :(得分:0)

您无法使用Attributes.Add添加事件绑定。您可以使用gridview中的RowCommand事件。这个link有一个使用RowCommand的好例子。 同样的问题here

答案 2 :(得分:0)

为您的链接按钮指定一个命令名称,如下所示

CommandName="Show" 

并在后面的代码中,将其处理为,

    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
        }
    }

答案 3 :(得分:0)

试试这一行:

 Dim WithEvents lbtnCountDetails As New LinkButton
相关问题