Enhancing the FindControl method

   —   
This article describes how to create a FindControl method which traverses the whole controls hierarchy to search for the given control. This approach is necessary, since the standard .NET FindControl method searches only one level deep in the hierarchy which is often insufficient due to the complex control trees generated by Kentico CMS.
In this example, we will be working with the EditableImage web part. We want to get the given control from an unknown location in the controls tree hierarchy and access its ImageUrl property. At first, we need to create two recursive methods searching through the controls tree. The first one travels to the root of the tree, which is a control without any parents:

// Recursively finding the root control in the control tree and checking the first hierarchy level for the searched control
    public Control RecFindControl(Control parent, string id)
    {
   //Checking if the control can’t be found on the current level
        Control c = parent.FindControl(id); 
        if (c == null && parent.Parent != null)
        {
  //The control wasn’t found, we need to search in higher levels
            c = RecFindControl(parent.Parent, id);
        }
        //We found our control and we don’t need to search further
        if (c != null)
        {
            return c;
        }
        // We arrived at the root but we didn’t find anything. We need to travel down the control tree to check all the nodes of the tree
        return RecDownFindControl(parent, id);        
    }


While ascending in the tree, we search the current level for our control. If we are lucky, it will be found in this traversal. If that’s not the case, we need to descent through all the nodes with a second recursive method, which will visit all the neighbor nodes that were skipped during the first search.
Here is the code for the second method doing the second traversal:

//Traveling down the control tree and searching for our control
    public Control RecDownFindControl(Control parent, string id)
    {
   //Checking all the available controls
        foreach (Control item in parent.Controls)
        {
            Control c = item.FindControl(id);
            if (c != null)
            {
      //We found our control on this level
                return c;
            }
  //Traveling further down if nothing was found
            RecDownFindControl(item, id);
        }
   //If there is no such control in any of the branches the method returns the null value
        return null;
    }



Now we can proceed to get the property mentioned in the beginning of the article. In this example, the control ID of the EditableImage web part is set to EditableImageCustom:

Control c = RecFindControl(this.Parent, "EditableImageCustom");
       
string imageUrl = ((System.Web.UI.WebControls.Image)c.Controls[0]).ImageUrl;


The imageUrl variable should contain the image URL of the given Editable Image web part after the execution of this code. Please note that you will have to test the mentioned code if it will work always as expected in your environment and perform additional safety checks, for example for uninitialized objects.

-bp-


Applies to: Kentico CMS 2.3a and later
Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.