[NOTE: This was originally posted at DevAuthority.com on 7/19/2005.]
I was recently tasked with applying some logic to an existing page where a datagrid was used to add and edit data. This is nothing uncommon mind you, but I have (believe it or not) never used controls in the footer template. The task involved determining whether specific controls were visible or not at runtime. Since the footer template does not belong to the items collection I would be unable to see this using the EditItemIndex property. After jumping through hoops I found that the way to gain access to a control in the footer. In the following example I’m looking for a LinkButton, but the process could easily be used to find any control: I just thought others may be able to benefit from this basic principle that I seemed to struggle with.
Dim footerIndex As Integer = Me.MyDataGrid.Controls(0).Controls.Count - 1
Dim myButton As LinkButton
myButton = CType(Me.MyDataGrid.Controls(0).Controls(footerIndex).FindControl("cmdThisButtonsID"), LinkButton)