While debugging a problem I’m having on a DetailsView finding a control in one of the templates I encountered something strange. I have implemented a recursive FindControl Extension, and it where finding a control with an id completely different from the one I where searching for. the implementation is basically calling Findcontrol on the parent control, and afterwards if nothing where found, calling the recursive function on the child controls.
I started digging into the asp.net code with reflector and found out how the implementation for a checkboxs FindControl method where (The one in System.Web.UI.WebControls.CheckBoxList)
protected override Control FindControl(string id, int pathOffset) { return this; }
This now all makes sense, why my FindControl found a CheckBoxList, I can however see no reasoning behind this implementation, could anyone enlighten me?
This implementation of FindControl is overriding a recursive method.
Is overriding:
I would speculate it would be used when the recursive method is not needed IE it is known that the current control is the one you are looking for.
extra reading MSDN
id
The identifier for the control to be found.
pathOffset
The number of controls up the page control hierarchy needed to reach a naming container.
link