EDIT: My guess is that you have some other control within your page that also has the ID "Comp1A", and that your FindControlRecursive
method is finding that control first. It's probably giving you null because that control, whatever it is, is not a DropDownList. When you do as DropDownList
the result would be null in that case.
Here is what I know about FindControl
, in case this is of any help to you.
FindControl is limited to controls within the same naming container (i.e. Parent/Ancestor control that implements the INamingContainer interface). If you are trying to find a control that is located inside of another control that is a naming container relative to the control on which you call the FindControl
method, then it won't find it.
A Page is a naming container, as is a UserControl and a ContentPlaceHolder. I think that TabContainer is also a naming container, as well as each tab control within the TabContainer.
EDIT2: Repeater and RepeaterItem (each "row" of your repeater will be a RepeaterItem) are both naming containers as well. This means that you really can't reliably find a control that is nested within a repeater if you start looking from the top (i.e. the page). You need to set your starting point from within that same RepeaterItem (essentially this is what James Johnson suggested). If you need more help on this, then you will need to provide a little more information about the context where you are executing target = FindDropDownListControl("Comp1A");
.
Your code is starting from the page and trying to dig down to find the "Comp1A" DropDownList. If this control were just a normal control within your "CE1" user control, then you could find it with something like the following:
this.Master.Master.FindControl("MainContent").FindControl("ContentPlaceHolder1").FindControl("TabContainer1").FindControl("tab1").FindControl("CE1").FindControl("Comp1A")
(Yikes! That's too long. See further below for shorter syntax.)
The master page also acts as a naming container, so that's why I started with this.Master
instead of this.Page
.
It looks like you are using a Master page within another Master page, therefore I've updated my example to use this.Master.Master
.
According to Jeff's post, you can accomplish the same thing using the following syntax:
this.Master.Master.FindControl("MainContent:ContentPlaceHolder1:TabContainer1:tab1:CE1:Comp1A")
However, as mentioned above, the control you are trying to find is inside of a repeater. One thing you could do is iterate over all of the items in the repeater, like this:
Control repeater = this.Master.Master.FindControl("MainContent:ContentPlaceHolder1:TabContainer1:tab1:CE1:Repeater1");
foreach (Control control in repeater.Controls)
{
var button = control.FindControl("Comp1A");
}
But if you are looking for one particular "Comp1A" DropDownList control from a particular row of the repeater, then you will need to utilize your context in order to use the correct root control for your search.