如何获取(FindControl)RadGrid 中 GridAttachmentColumn 的按钮

How to I get (FindControl) the button of a GridAttachmentColumn in a RadGrid

我有一个带有名为 "FileName" 的 GridAttachmentColumn 的 RadGrid。我试图在 ItemCreated 事件中从 GridDataItem 中获取(FindControl)控件。具体来说,我想要按钮控件(在本例中为 linkBut​​ton)。 item.FindControl("FileName")总是returns没有。

    Protected Sub AttachmentsRadGrid_ItemCreated(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
            If item IsNot Nothing Then
                Dim FileName = item.FindControl("FileName") 'Always Nothing
                If FileName IsNot Nothing Then
                    'Do something with it
                End If
            End If
        End If
    End Sub
Dim button As LinkButton = TryCast(item("FileName").Controls(0), LinkButton)

Dim FileName = item.FindControl("gac_FileName")

第一行代码可能是 Telerik's preference 所以我把那行放在第一位。请注意,读取模式下的 AttachmentColumn 基本上只是一个链接按钮。

注意在第二个例子中,"gac_" in item.FindControl("gac_FileName")被添加到列的UniqueName前面。当我从浏览器检查元素时,我在 Chrome DevTools 中注意到了它。我应该注意 "FileName" 是列的唯一名称,以防您不想通读上面的代码。

更安全的方法,Telrik 的首选方法是按名称而不是索引调用控件...

将按钮调暗为 LinkBut​​ton = TryCast(item("FileName").Controls("gac_FileName"), LinkBut​​ton)