无法在 FormView 中通过 FindControl 访问 CheckListBox
本文关键字:FindControl 访问 CheckListBox FormView | 更新日期: 2024-10-26 07:06:10
>我有这样的表单视图和formview_databound()方法:
<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;
}
但blsCatlist
是null
.我真的很困惑!因为可以在formview_Inserting() 事件中找到它,但在更改模式和更改模式下找不到它并且引用为空。
实际上,我想在ItemTemplate中绑定一个项目符号列表,在EditTemplate中绑定一个复选框列表。
>仅当控件直接包含在指定的容器中时,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;
}
FormView
上的FindControl
仅适用于 FormView 的"当前模式"属性设置为的模板。
在您的情况下,如果 FormView 设置为只读模式,则只能对"项目符号列表"执行 FindControl,因为这是控件所在的模板。