【发布时间】:2011-03-12 08:41:00
【问题描述】:
我使用以下方法递归查找asp.net页面上的控件:
/// <summary>
/// Searches recursively for a server control with the specified id parameter.
/// </summary>
/// <param name="start">The start.</param>
/// <param name="id">The id.</param>
/// <returns>A <see cref="Control"/></returns>
public static Control FindControl(Control start, string id)
{
Control foundControl;
if (start == null)
return null;
foundControl = start.FindControl(id);
if (foundControl != null)
return foundControl;
foreach (Control c in start.Controls)
{
foundControl = FindControl(c, id);
if (foundControl != null)
return foundControl;
}
return null;
}
我遇到了一个问题,因为它返回了错误的控件。我将问题追溯到标准的 FindControl 方法,并通过检查返回的控件的 id 确实与请求的匹配来修复它,如下所示:
foundControl = start.FindControl(id);
if (foundControl != null && foundControl.ID == id)
return foundControl;
我的问题是为什么 start.FindControl(id) 返回的控件与请求的 id 不匹配?
【问题讨论】:
标签: asp.net web-controls