I ran into a situation where FindControl was returning a control that wasn’t a complete match of the Id I was searching by. There are two controls in the parentcontrol with similar Ids like: “MyControl” and “MyControlAlternate”. When I call FindControl(“MyControl”) the control returned is “MyControlAlternate”. I was wondering if anyone had any explaination why or thoughts as to what might be causing this problem. The way I got around this was implementing my own version of FindControl.
I ran into a situation where FindControl was returning a control that wasn’t a
Share
The default behavior of FindControl to use its own naming container. If that doesn’t work it tries to do pattern matching on the name. That would explain why you are getting the wrong control – your naming container is incorrect and the code is making it into the pattern matching phase.
Here is the dissassembly of FindControl from Reflector:
Clearly what is happening in your example is that you are not specifying the correct naming container. However, without providing a code example I cannot be more specific than this.