我有一个复杂的 asp.net 表单,在一个表单中甚至有 50 到 60 个字段,就像这样Multiview
,在 MultiView 里面我有一个GridView
,在 GridView 里面我有几个CheckBoxes
.
目前我正在使用链接FindControl()
方法并检索孩子ID。
现在,我的问题是是否有其他方法/解决方案来查找 ASP.NET 中的嵌套控件。
如果您正在寻找一种特定类型的控件,您可以使用像这样的递归循环 -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);
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)