views:

389

answers:

1

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?

+1  A: 

This implementation of FindControl is overriding a recursive method.

protected override Control FindControl(string id, int pathOffset)
{
    return this;
}

Is overriding:

protected virtual Control FindControl(string id, int pathOffset)
{
     string str;
     this.EnsureChildControls();
if (!this.flags[0x80])
{
     Control namingContainer = this.NamingContainer;
if (namingContainer != null)
{
    return namingContainer.FindControl(id, pathOffset);
}

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

Control.FindControl searches the current naming container for a server control with the specified id and an integer, specified in the pathOffset parameter, which aids in the search. You should not override this version of the FindControl method.

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

cgreeno