2013-12-18 121 views
0

訪問CheckListBox通過的FindControl我有這樣的表單視圖和formview_databound()方法:不能在FormView控件

<asp:FormView ID="frm" runat="server" DataKeyNames="Id" DataSourceID="dsSelectedProduct"> 
     <ItemTemplate> 
      <asp:BulletedList ID="blstCatlist" DataTextField="Name" DataValueField="Id" runat="server"></asp:BulletedList> 
    </itemTemplate> 
</asp:FormView> 

if (frm.CurrentMode == FormViewMode.ReadOnl 
{ 
    var blstCatlist = frm.FindControl("blstCatlist") as BulletedList; 
} 

blsCatlistnull。 我真的很困惑!因爲可以找到它formview_Inserting()事件,但在ChangingMode和changedMode無法找到它並且引用爲空。

其實我想在ItemTemplate和EditTemplate中的CheckBoxList中綁定一個bulletList。

回答

0

FindControl只有在控件被指定的容器直接包含時纔會找到控件;該方法不會搜索整個控件內控件的層次結構。

要在不知道其直接容器時找到控件,您需要一個自定義方法來搜索控件層次結構中的控件。

public static Control FindControlRecursive(Control rootControl, string controlID) 
{ 
    if (rootControl.ID == controlID) return rootControl; 

    foreach (Control controlToSearch in rootControl.Controls) 
    { 
     Control controlToReturn = FindControlRecursive(controlToSearch, controlID); 
     if (controlToReturn != null) return controlToReturn; 
    } 
    return null; 
} 

FindControl上一個FormView將只用於FormView的「CURRENTMODE」屬性設置爲模板的工作。

在你的情況,你只能爲「的BulletedList」如果你的FormView控件設置爲只讀模式做的FindControl,因爲那是你的控制中存在的模板。

+0

親愛@giammin,我知道這一點。它不起作用。我利用你的解決方案 – Mohammadreza

+0

@Edalat無論如何,你的控件不在插入模式的模板中 – giammin

相關問題