ASP.Net / C#,遍历网页上的某些控件? [英] ASP.Net / C#, Loop through certain controls on a page?

查看:178
本文介绍了ASP.Net / C#,遍历网页上的某些控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过所有的控制循环我的网页上,并在一定条件下设置某些类型(文本框,复选框的DropDownList,等等)来启用=假。
不过,我注意到一个明显的页面负荷增加循环这样。是否有可能只得到某些类型的控制从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?

推荐答案

这不能完全使用LINQ做,但你可以有这样的定义的扩展

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

和电话

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

这篇关于ASP.Net / C#,遍历网页上的某些控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆