FindControl - can't find dropdownlist

12,505

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");
Share:
12,505
Oedum
Author by

Oedum

Updated on June 27, 2022

Comments

  • Oedum
    Oedum almost 2 years

    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?

    • huMpty duMpty
      huMpty duMpty over 12 years
      why you using Page.Findcontrol to access the dropdown? why don't you use the "ddlGoalKeeper" to set the visible
    • V4Vendetta
      V4Vendetta over 12 years
      Whatz wrong in using ddlGoalKeeper.Visible ?
    • Shadow The Kid Wizard
      Shadow The Kid Wizard over 12 years
      It's most likely part of Repeater, or something like this. In such case you won't find it with such logic, you'll have to use the ItemDataBound event and handle the control in there.
    • Oedum
      Oedum over 12 years
      I want to change the string = ddlGoalKeeper, and I don't want to make 28 different if statements to set all my ddl to visible