如何禁用在ASP.NET页面的所有控件? [英] How do I disable all controls in ASP.NET page?

查看:171
本文介绍了如何禁用在ASP.NET页面的所有控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面多个DropDownList的,并想如果用户选择一个复选框,它读取禁用所有禁用所有。到目前为止,我有这样的代码,这是行不通的。有什么建议?



 的foreach(在this.Page.Controls控制C)
{
如果(C 。是的DropDownList)
((DropDownList的)(C))启用=假;
}


解决方案

每个控件的子控件,所以你需要使用递归来达到他们都:

 保护无效DisableControls(控制父母,布尔州){
的foreach(在parent.Controls控制三){
如果(c是DropDownList的){
((DropDownList的)(C))启用=状态。
}

DisableControls(C,国);
}
}



然后调用它像这样:

 保护无效EVENT_NAME(...){
DisableControls(页,假); //使用任何最顶部的控制了所有的下拉列表,或只是网页控制
} // div的,表格等,可以通过添加=服务器属性


I have multiple dropdownlist in a page and would like to disable all if user selects a checkbox which reads disable all. So far I have this code and it is not working. Any suggestions?

foreach (Control c in this.Page.Controls)
{
    if (c is DropDownList)
        ((DropDownList)(c)).Enabled = false;
}

解决方案

Each control has child controls, so you'd need to use recursion to reach them all:

protected void DisableControls(Control parent, bool State) {
    foreach(Control c in parent.Controls) {
        if (c is DropDownList) {
            ((DropDownList)(c)).Enabled = State;
        }

        DisableControls(c, State);
    }
}

Then call it like so:

protected void Event_Name(...) {
    DisableControls(Page,false); // use whatever top-most control has all the dropdowns or just the page control
} // divs, tables etc. can be called through adding runat="server" property

这篇关于如何禁用在ASP.NET页面的所有控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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