How do I disable all controls in ASP.NET page?

后端 未结 9 1673
旧时难觅i
旧时难觅i 2020-12-14 08:17

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 s

9条回答
  •  清歌不尽
    2020-12-14 09:05

    I was working with ASP.Net and HTML controls I did like this

    public void DisableForm(ControlCollection ctrls)
        {
            foreach (Control ctrl in ctrls)
            {
                if (ctrl is TextBox)
                    ((TextBox)ctrl).Enabled = false;
                if (ctrl is Button)
                    ((Button)ctrl).Enabled = false;
                else if (ctrl is DropDownList)
                    ((DropDownList)ctrl).Enabled = false;
                else if (ctrl is CheckBox)
                    ((CheckBox)ctrl).Enabled = false;
                else if (ctrl is RadioButton)
                    ((RadioButton)ctrl).Enabled = false;
                else if (ctrl is HtmlInputButton)
                    ((HtmlInputButton)ctrl).Disabled = true;
                else if (ctrl is HtmlInputText)
                    ((HtmlInputText)ctrl).Disabled = true;
                else if (ctrl is HtmlSelect)
                    ((HtmlSelect)ctrl).Disabled = true;
                else if (ctrl is HtmlInputCheckBox)
                    ((HtmlInputCheckBox)ctrl).Disabled = true;
                else if (ctrl is HtmlInputRadioButton)
                    ((HtmlInputRadioButton)ctrl).Disabled = true;
    
                DisableForm(ctrl.Controls);
            }
        }
    

    called like this

    DisableForm(Page.Controls);
    

提交回复
热议问题