san*_*ngh 46 asp.net findcontrol
我有一个复杂的asp.net表单,在一个表单中甚至有50到60个字段Multiview,在MultiView中我有一个GridView,而在GridView中我有几个CheckBoxes.
目前我正在使用该FindControl()方法的链接并检索子ID.
现在,我的问题是,是否有任何其他方法/解决方案可以在ASP.NET中找到嵌套控件.
jim*_*mig 72
如果你正在寻找一种特定类型的控件,你可以使用像这样的递归循环 - 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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*rke 18
像往常一样晚.如果有人仍然对此感兴趣,那么有许多相关的SO 问题和答案.我的版本的递归扩展方法解决了这个问题:
public static IEnumerable<T> FindControlsOfType<T>(this Control parent)
where T : Control
{
foreach (Control child in parent.Controls)
{
if (child is T)
{
yield return (T)child;
}
else if (child.Controls.Count > 0)
{
foreach (T grandChild in child.FindControlsOfType<T>())
{
yield return grandChild;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
所有突出显示的解决方案都使用递归(性能代价高昂).这是没有递归的更清洁的方式:
public T GetControlByType<T>(Control root, Func<T, bool> predicate = null) where T : Control
{
if (root == null) {
throw new ArgumentNullException("root");
}
var stack = new Stack<Control>(new Control[] { root });
while (stack.Count > 0) {
var control = stack.Pop();
T match = control as T;
if (match != null && (predicate == null || predicate(match))) {
return match;
}
foreach (Control childControl in control.Controls) {
stack.Push(childControl);
}
}
return default(T);
}
Run Code Online (Sandbox Code Playgroud)
FindControl不会递归地在嵌套控件中搜索.它只找到NamigContainer是您正在调用FindControl的控件.
这是ASP.Net默认情况下不会递归查看嵌套控件的原因:
考虑到您希望将其中的GridView,Formviews,UserControl等封装在其他UserControl中,以实现可重用性.如果您已经在页面中实现了所有逻辑并使用递归循环访问这些控件,那么很难重构它.如果您已经通过事件处理程序(GridView的RowDataBound)实现了逻辑和访问方法,那么它将更简单,更不容易出错.
| 归档时间: |
|
| 查看次数: |
103412 次 |
| 最近记录: |