【问题标题】:Click event is not firing of dynamically added link button in grid view单击事件不会触发网格视图中动态添加的链接按钮
【发布时间】:2011-12-16 07:30:27
【问题描述】:

我正在网格视图的行数据绑定上编写以下代码,但我没有收到链接按钮的点击事件

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

【问题讨论】:

标签: asp.net vb.net


【解决方案1】:

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

ElseIf e.Row.RowType = DataControlRowType.DataRow Then

其实你忘了给LinkBut​​ton1的事件添加事件处理函数,所以无法获取到LinkBut​​ton的点击事件。

你必须做出的改变:-

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

试试看。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      给你的链接按钮一个命令名称,像这样

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

      【讨论】:

        【解决方案4】:

        试试这条线:

         Dim WithEvents lbtnCountDetails As New LinkButton
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-27
          • 1970-01-01
          • 2018-01-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多