我目前正在浏览页面上的所有控件,并在某些条件下将某些类型(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/