3

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?

Curtis
  • 101,612
  • 66
  • 270
  • 352
Oedum
  • 796
  • 2
  • 9
  • 28
  • 1
    why you using Page.Findcontrol to access the dropdown? why don't you use the "ddlGoalKeeper" to set the visible – huMpty duMpty Nov 30 '11 at 11:15
  • 1
    Whatz wrong in using `ddlGoalKeeper.Visible` ? – V4Vendetta Nov 30 '11 at 11:16
  • 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. – Shadow The GPT Wizard Nov 30 '11 at 11:16
  • 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 – Oedum Nov 30 '11 at 11:51

3 Answers3

5

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");
Jay
  • 5,897
  • 1
  • 25
  • 28
0

Why not setting ddlGoalKeeper.Visible = true; directly?

Shai
  • 25,159
  • 9
  • 44
  • 67
0

I am not sure why you are trying to use FindControl, if you want to toggle the visibility the simplest way would be to use ddlGoalKeeper.Visible since the control is available.

In case if its contained in some other control say a grid then you would have to find it in the Parent control like in a particular row of the grid [gridrow1].FindControl("ddlGoalKeeper") then it would make more sense.

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82