FindControl - can't find dropdownlist

Oedum picture Oedum · Nov 30, 2011 · Viewed 12.1k times · Source

I have a dropdownlist:

<asp:DropDownList ID="ddlGoalKeeper" runat="server">
                </asp:DropDownList>

A nice little one. I have some code to find it:

DropDownList myControl1 = (DropDownList)Page.FindControl("ddlGoalKeeper");

Not.. it's just that my myControl1 doesn't get set... so when i later in my code try to set visible to true, it doesn't work.

Any ideas?

Answer

Jay picture Jay · Nov 30, 2011

One reason I have run in to for that not to work is if the control is when the site uses a master page.

You can use this idea to get a reference first to the master page and then get the right control from the content page:

 ContentPlaceHolder MainContent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
 DropDownList myControl1 = (DropDownList)MainContent.FindControl("ddlGoalKeeper");