Des*_*tor 5 c# asp.net exception
编辑:查看底部的原因是为什么会发生这种情况
我有一个非常奇怪的IndexOutOfRangeException(如标题中所述).当我使用foreach迭代控件的控件(递归FindControl)时会发生这种情况.
然后我想添加一个额外的检查,确保root.Controls.Count > 0
.但是我一直得到异常,而调试器清楚地说Count == 0
.
有问题的根是FormView.如果有人有任何想法为什么简单的属性检查抛出IndexOutOfRangeException,请赐教!
异常stackTrace(是的,它是完整的):
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
Run Code Online (Sandbox Code Playgroud)
码:
public static Control FindControlRecursive(this Control root, string id)
{
if (root.ID == id)
{
return root;
}
if (root.Controls.Count > 0)
{
foreach (Control c in root.Controls)
{
Control t = c.FindControlRecursive(id);
if (t != null)
{
return t;
}
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我试图使用本机FindControl
函数,这会引发相同的错误.
具体问题:
foreach如何在本机集合上抛出IndexOutOfRangeException.
EDIT2:
不知怎的,这似乎与使用ObjectDataSource有关,我没有正确填写输入参数,但我没有得到任何错误.也许这会以某种方式破坏FormView(即使用该数据源).我仍然有兴趣知道在访问childcontrols之前如何在没有错误的情况下发生这种情况.