ASP.Net / C#,循环访问页面上的某些控件?

发布于 2024-10-19 19:33:22 字数 170 浏览 1 评论 0原文

我当前正在循环访问页面上的所有控件,并在某些条件下将某些类型(TextBox、CheckBox、DropDownList 等)设置为 Enabled=False。 但是我注意到明显的页面加载增加循环如下。是否可以仅从 Page.Controls 对象获取某些类型的控件,而不是循环遍历所有控件?可能是像 LINQ 这样的东西?

I am currently looping through all the controls on my page and setting certain types (TextBox, CheckBox, DropDownList, etc.) to Enabled=False under certain conditions.
However I notice an obvious page load increase looping like this. Is it possible to only get certain types of controls from the Page.Controls object rather than Loop through them all? Possibly with something like LINQ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

友欢 2024-10-26 19:33:22

这不能完全使用 LINQ 完成,但您可以像这样定义扩展

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;
                }
            }
        }
    }

并调用

this.GetAllControls().OfType<TextBox>().ToList().ForEach(t => t.Enabled = false);

This cannot be done entirely using LINQ but you could have an extension defined like this

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

this.GetAllControls().OfType<TextBox>().ToList().ForEach(t => t.Enabled = false);
秋意浓 2024-10-26 19:33:22

您可以循环遍历所有控件(嵌套控件):

private void SetEnableControls(Control page, bool enable)
{
    foreach (Control ctrl in page.Controls)
    {
        // not sure exactly which controls you want to affect so just doing TextBox
        // in this example.  You could just try testing for 'WebControl' which has
        // the Enabled property.
        if (ctrl is TextBox)
        {
            ((TextBox)(ctrl)).Enabled = enable; 
        }

        // You could do this in an else but incase you want to affect controls
        // like Panels, you could check every control for nested controls
        if (ctrl.Controls.Count > 0)
        {
            // Use recursion to find all nested controls
            SetEnableControls(ctrl, enable);
        }
    }
}

然后只需使用以下命令初始调用它即可禁用:

SetEnableControls(this.Page, false);  

You could loop through all the control (an nested ones):

private void SetEnableControls(Control page, bool enable)
{
    foreach (Control ctrl in page.Controls)
    {
        // not sure exactly which controls you want to affect so just doing TextBox
        // in this example.  You could just try testing for 'WebControl' which has
        // the Enabled property.
        if (ctrl is TextBox)
        {
            ((TextBox)(ctrl)).Enabled = enable; 
        }

        // You could do this in an else but incase you want to affect controls
        // like Panels, you could check every control for nested controls
        if (ctrl.Controls.Count > 0)
        {
            // Use recursion to find all nested controls
            SetEnableControls(ctrl, enable);
        }
    }
}

Then just call it initally with the following to disable:

SetEnableControls(this.Page, false);  
唔猫 2024-10-26 19:33:22

我喜欢此链接中的解决方案 LINQ 相当于 IEnumerable的 foreach

对我来说效果很好!

您可以使用像这样的 linq 查询来进行控件的迭代,

    (from ctrls in ModifyMode.Controls.OfType<BaseUserControl>()

                 select ctrls).ForEach(ctrl => ctrl.Reset());

这里 BaseUserControl 是我所有控件都使用的基类,您可以在这里很好地使用 Control 本身,并且扩展方法允许您组合迭代和执行。

I like the solution in this link LINQ equivalent of foreach for IEnumerable<T>

Worked quite well for me!

You can do the iteration of controls using a linq query like

    (from ctrls in ModifyMode.Controls.OfType<BaseUserControl>()

                 select ctrls).ForEach(ctrl => ctrl.Reset());

Here BaseUserControl is a base class which all my controls use, you could very well use Control itself here, and the extension method allows you to club iteration and execution.

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