0

I've got a RadGrid with a GridAttachmentColumn named "FileName". I'm trying to get (FindControl) the control out of the GridDataItem in the ItemCreated event. Specifically, I want the button control (or linkButton in this case). item.FindControl("FileName") always returns Nothing.

    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
Tony L.
  • 17,638
  • 8
  • 69
  • 66

2 Answers2

1
Dim button As LinkButton = TryCast(item("FileName").Controls(0), LinkButton)

OR

Dim FileName = item.FindControl("gac_FileName")

The first line of code might be Telerik's preference so I put that line first. Notice that the AttachmentColumn in read mode is basically just a linkbutton.

Notice in the 2nd example that "gac_" in item.FindControl("gac_FileName") is added to the front of the UniqueName of the column. I noticed it in Chrome DevTools when I inspected the element from the browser. I should note that "FileName" is the UniqueName of the column in case you didn't want to read through the code above.

Tony L.
  • 17,638
  • 8
  • 69
  • 66
0

Safer method, and Telrik's preferred method is to call the control by name rather than index...

Dim button As LinkButton = TryCast(item("FileName").Controls("gac_FileName"), LinkButton)

Damian70
  • 348
  • 1
  • 3
  • 18
  • Do you have a link to any documentation that supports that? If so, please supply it because I think it would be well received here. I like the call to the name of the control but Telerik support also suggested using item("FileName").Controls(0) and the samples in the link I provided also use them. – Tony L. Mar 15 '15 at 03:30
  • I'm not able to provide you a link to our support tickets, you can use the control index or name, either will work. The advantage of using the name is that you always know that you referring to the correct control, I also noticed in the line of code that there is a type Controls(0) should be FindControl("gac_Filename") – Damian70 Mar 15 '15 at 08:32
  • That is the line that was provided by Telerik support and is in their documentation so I figured I should include it as well. I also haven't seen any documentation that says the name of the control will have "gac_" underscore in front of it. I agree in that I prefer calling the control by name better but I deferred to Telerik. – Tony L. Mar 15 '15 at 14:32
  • You don't need to precede it with anything Tony, if the control was called FirstName, you would of course use that. – Damian70 Mar 15 '15 at 14:57