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

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

问题描述

我在一个页面中有多个下拉列表,如果用户选择一个复选框,该复选框显示禁用所有,我想禁用所有.到目前为止,我有这段代码,但它不起作用.有什么建议吗?

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

然后这样称呼它:

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天全站免登陆