Search code examples
c#asp.netdrop-down-menufindcontrol

FindControl - can't find dropdownlist


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?


Solution

  • 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");