问题描述
我有一个复杂的asp.net的形式,具有一种形式,甚至50到60场像有多视点
,里面的MultiView我有一个 GridView控件
了,里面的GridView我有几个的CheckBox
。
I have a complex asp.net form,having even 50 to 60 fields in one form like there is Multiview
, inside MultiView I have a GridView
, and inside GridView I have several CheckBoxes
.
目前我使用的是的FindControl()
方法的链接和检索子ID。
Currently I am using chaining of the FindControl()
method and retrieving the child ID.
现在,我的问题是有任何其他方式/解决方案找到ASP.NET中的嵌套控制。
Now, my question is that is there any other way/solution to find the nested control in ASP.NET.
推荐答案
如果你正在寻找一个特定类型的控制,你可以使用一个递归循环像这样的 -
http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx
If you're looking for a specific type of control you could use a recursive loop like this one -http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx
下面是我做的,返回给定类型的所有控件的例子
Here's an example I made that returns all controls of the given type
/// <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);
}
}
}
}
这篇关于更好地找到ASP.NET控制方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!