Is the object you're seeking nested in a container that is itself nested within the control being searched? I discovered that the loop in my original example did not search the contents of any containers in any Objects found in span1 in the For loop. Page.FindControl suffers from the same problem. I had to develop a recursive control search to find any object in any control nested within the root control. It's not clear which language you're using, but this
VB code is being used in a production application:
Public Function FindControlRecursive(v_ctlRoot As Control, _
v_strControlIdSought As String) As Control
dim strRootID As String = v_ctlRoot.ID
Try
If strRootID = v_strControlIdSought Then
Return v_ctlRoot ' exit immediately if the root is the control sought
End If
' loop through all of the controls in the current container
For Each ctlWorking As Control In v_ctlRoot.Controls
Dim strControlClientID As String = ctlWorking.ClientID
If strControlClientID = v_strControlIdSought Then ' if the current control is the one sought
Return ctlWorking ' exit here returning the control found
End If
If ctlWorking.Controls.Count > 0 Then ' if the current control is a container
' call this function recursively passing this control
Dim ctlFound As Control = FindControlRecursive(ctlWorking, _
v_strControlIdSought)
If ctlFound IsNot Nothing Then ' if a control was found in the recursive call above, no need to search further
Return ctlFound ' return the control to whatever called this iteration of the function
End If
End If
Next
Return Nothing ' if the control was not found in the current iteration
Catch
m_strError = Err.Description ' return error in module level variable
Return Nothing
End Try
End Function
Pass it the Form or any container on the Form as a control reference in ctlRoot and the ID of the control sought in strControlID. It returns a reference to the control sought, if it was found, which can then be used to change the control's attributes. It returns nothing if the control could not be found anywhere below the root control initially passed, so if you're sure it's there somewhere but cannot find it, start at the Form level.