Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3921
  • Last Modified:

ASP.NET FindControl method fails to find a control

In an C# ASP.NET Web application I am developing, I am making extensive use of nested Web and Custom controls. The Control.FindControl(id) methods is clearly very important in locating subcontrols by id rather than assuming an actual location in the control tree.

However, it appears that it can't always be relied on. Here is a case in point:

In the following control tree fragment:

<span id="ctl00_MstCntPlcHld_VwrPnl_EntityViewer_Ftr_Pgr">
  <div id="ctl00_MstCntPlcHld_VwrPnl_EntityViewer_Ftr_Pgr_ctl00_Pnl">
      <table id="ctl00_MstCntPlcHld_VwrPnl_EntityViewer_Ftr_Pgr_ctl00_PgrTbl" cellspacing="0" cellpadding="0" border="0" frame="void" width="790px" style="height:22px">
      <tr class="Grid_Footer" style="height:22px">
        <td class="Dim_22x22"></td>
        <td class="Dim_22x22"></td>
...
        <td class="Dim_22x22"></td>
        <td class="Dim_22x22"></td>
        <td class="Dim_18x22"></td>
      </tr>
    </table>
  </div>
</span>

When executed in the control <span id="ctl00_MstCntPlcHld_VwrPnl_EntityViewer_Ftr_Pgr">, FindControl("PgrTbl") fails to find the enclosed HtmlTable, returning null. Note that no type conversion is performed.

However, the simple following alternative static method does return the correct table:

        public static Control FindControlByID(Control parent, String id)
        {
            if (parent == null)
            {
                return null;
            }
            foreach (Control control in parent.Controls)
            {
                if (control.ID == id)
                {
                    return control;
                }
                Control child = FindControlByID(control, id);
                if (child != null)
                {
                    return child;
                }
            }
            return null;
        }

Could anyone tell me whether I have overlooked a feature of FindControl or whether this is a bug?

Thanks,

David
0
Sunningwell
Asked:
Sunningwell
  • 2
  • 2
  • 2
1 Solution
 
McExpCommented:
It's no bug, as you have said your recusive findControl provides you with the functionality you need. I would guess that the defualt FindControl function was not implimeneted in the way you have described for performance reasons, or as MS have done before they simply didn't realise how people would want FindControl to work

The MSDN definition of FindControl is "Searches the current naming container for the specified server control." They made no attmept to recusrivly search all child naming containers.
0
 
samtran0331Commented:
Are you dynamically adding some of these controls?...or trying to do the FindControl from a master page?
The only time I've had to use a recursive function like what you posted are in those scenarios above...and it's not a bug or a missing FindControl feature...
basically...with dynamic controls...the controls collection isn't exactly what you think it is...FindControl is "failing" because it's not driving down deep enough into the collection tree...the recursive function makes sure it does
0
 
SunningwellAuthor Commented:
The controls are created either dynamically (from code) or via templates (loaded dynamically). Your explanation sounds right. However, I remain unsure as to what 'version' of the control tree FindControl is looking at and how deep it is going. Seems a little useless! All my controls are rendered perfectly well.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
McExpCommented:
It doens't go deep, it only "Searches the current naming container for the specified server control" without recursing into child objects.

Imagine if you had a very complicated structure with deep nesting it could be a very time consuming operation if the default functionality was to search in every child object. Hence the basic functionality searches the current naming container only.

And why we also begin to impliment our own functionality when we require it, and understand our control structures.
0
 
samtran0331Commented:
I guess you both mis-understood what I meant by "going deep"...in your "FindControlByID" function...

 foreach (Control control in parent.Controls)
            {
....
                Control child = FindControlByID(control, id);
....
            }



The "foreach" goes into the control.
Recursively calling the function within itself by calling "FindControlByID"

Those 2 elements of the function...if you had a control nested in a control nested in a control
it's going to "go deep" into each level.

As far as:

>>"However, I remain unsure as to what 'version' of the control tree FindControl is looking at"
This would depend on when you create the dynamic controls in relation to when you use FindControlByID

>>"Seems a little useless! All my controls are rendered perfectly well."
Yes, the page will render, but without the recursive function, you wouldn't be able to set or get the value of any dynamically created controls.



0
 
SunningwellAuthor Commented:
Thanks for your comments. The main thing it to understand what methods etc in such APIs actually do. I am often frustrated by the inadequate level of documentation. There is nothing in http://msdn2.microsoft.com/en-us/library/486wc64h.aspx to suggest the limitations on the search.
Now that I know, the solution is obvious.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now