我有一个复杂的asp.net表单,在一个表单中有50到60个字段,就像有Multiview
个一样,在MultiView中有GridView
个字段,在GridView中有CheckBoxes
个字段.
目前,我正在使用FindControl()
方法的链接并检索子ID.
现在,我的问题是,有没有其他方法/解决方案可以在ASP中找到嵌套控件.网
我有一个复杂的asp.net表单,在一个表单中有50到60个字段,就像有Multiview
个一样,在MultiView中有GridView
个字段,在GridView中有CheckBoxes
个字段.
目前,我正在使用FindControl()
方法的链接并检索子ID.
现在,我的问题是,有没有其他方法/解决方案可以在ASP中找到嵌套控件.网
如果您正在寻找特定类型的控件,您可以使用像这样的递归循环- http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx个
下面是我创建的一个示例,它返回给定类型的所有控件
/// <summary>
/// Finds all controls of type T stores them in FoundControls
/// </summary>
/// <typeparam name="T"></typeparam>
private class ControlFinder<T> where T : Control
{
private readonly List<T> _foundControls = new List<T>();
public IEnumerable<T> FoundControls
{
get { return _foundControls; }
}
public void FindChildControlsRecursive(Control control)
{
foreach (Control childControl in control.Controls)
{
if (childControl.GetType() == typeof(T))
{
_foundControls.Add((T)childControl);
}
else
{
FindChildControlsRecursive(childControl);
}
}
}
}