asp.net - ASP.Net/C#,在页面上的某些控件之间循环?

标签 asp.net linq loops

我目前正在浏览页面上的所有控件,并在某些条件下将某些类型(TextBox,CheckBox,DropDownList等)设置为Enabled = False。
但是,我注意到这样的页面加载循环明显增加。是否只能从Page.Controls对象中获取某些类型的控件,而不是通过它们全部循环?可能使用LINQ之类的东西吗?

最佳答案

不能完全使用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);

关于asp.net - ASP.Net/C#,在页面上的某些控件之间循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5122987/

相关文章:

.net - .NET 中您最喜欢的功能是什么?

c# - Calendar Extender 日期选择并转换为正确的格式

c# - EF Core SingleOrDefaultAsync 编译时返回类型错误

javascript - 为什么重复元素返回未定义?

javascript - 如何在js按键中插入点而不是逗号

c# - 在一个语句中分组和构建对象

c# - Linq 查询创建字符串数组错误列表

python - 如何 json 解析 python 输出并将其值存储在 dict 或变量中?

java - 如何在Java中旋转星号梯形?

c# - 获取 System.Xml.XmlException : Name cannot begin with the ' ' character, 十六进制值 0x20。第 42 行,第 36 位