FindControl 在我的递归方法中得到错误的控制

发布于 2024-10-21 07:44:05 字数 1179 浏览 3 评论 0原文

我使用以下方法递归地查找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2024-10-28 07:44:05

我使用

static class ControlExtension
    {
        public static IEnumerable<Control> GetAllControls(this Control parent)
        {
            foreach (Control control in parent.Controls)
            {
                yield return control;
                foreach (Control descendant in control.GetAllControls())
                {
                    yield return descendant;
                }
            }
        }
    }

并调用

var foundControl =  Page.GetAllControls().Where(c => c.ID = id);

EDIT:

而不是调用它来开始搜索

 foundControl = start.FindControl(id);

也许您应该开始

foundControl = FindControl(start, id);

I use

static class ControlExtension
    {
        public static IEnumerable<Control> GetAllControls(this Control parent)
        {
            foreach (Control control in parent.Controls)
            {
                yield return control;
                foreach (Control descendant in control.GetAllControls())
                {
                    yield return descendant;
                }
            }
        }
    }

and call

var foundControl =  Page.GetAllControls().Where(c => c.ID = id);

EDIT:

maybe instead of calling this to start the search

 foundControl = start.FindControl(id);

you should start with

foundControl = FindControl(start, id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文