FindControl 在我的递归方法中得到错误的控制
我使用以下方法递归地查找 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 匹配吗?
I use the following method to find a control on an asp.net page recursively:
/// <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;
}
I hit a problem because it was returning the wrong control. I tracked the problem down to the standard FindControl method, and fixed it by checking that the id of the control returned did actually match the one requested like this:
foundControl = start.FindControl(id);
if (foundControl != null && foundControl.ID == id)
return foundControl;
My question is why does start.FindControl(id) ever return a control that does not match the id requested?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用
并调用
EDIT:
而不是调用它来开始搜索
也许您应该开始
I use
and call
EDIT:
maybe instead of calling this to start the search
you should start with