2011-11-30 49 views
2

我有一個下拉列表:的FindControl - 找不到下拉列表

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

一個不錯的小傢伙。我有一些代碼找到它:

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

不..這只是我的myControl1不被設置......所以當我後來在我的代碼嘗試設置爲true可見,這是行不通的。

任何想法?

+1

爲什麼你使用Page.Findcontrol訪問下拉?爲什麼不使用「ddlGoalKeeper」來設置可見的 –

+1

使用'ddlGoalKeeper.Visible'時的錯誤? – V4Vendetta

+0

這很可能是Repeater的一部分,或者類似的東西。在這種情況下,你不會用這樣的邏輯找到它,你必須使用ItemDataBound事件並處理那裏的控件。 –

回答

4

我已經運行的一個原因是,如果控件是當網站使用母版頁時不起作用。

你可以使用這個想法首先得到一個參考的母版頁,然後得到的內容頁面的權限控制:

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

爲什麼不設置ddlGoalKeeper.Visible = true;直?

+0

這個問題或答案? – sasjaq

0

我不確定你爲什麼試圖使用FindControl,如果你想切換可見性,最簡單的方法是使用ddlGoalKeeper.Visible,因爲控件可用。

如果包含在其他控件中的某個網格包含一個網格,那麼您必須在網格控件中找到它,就像網格中的特定行[gridrow1].FindControl("ddlGoalKeeper")那樣它會更有意義。