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

后端 未结 9 1642
旧时难觅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);
    
    0 讨论(0)
  • 2020-12-14 09:08

    Here's a VB.NET version which also takes an optional parameter so it can be used for enabling the controls as well.

    Private Sub SetControls(ByVal parentControl As Control, Optional ByVal enable As Boolean = False)

        For Each c As Control In parentControl.Controls
            If TypeOf (c) Is CheckBox Then
                CType(c, CheckBox).Enabled = enable
            ElseIf TypeOf (c) Is RadioButtonList Then
                CType(c, RadioButtonList).Enabled = enable
            End If
            SetControls(c)
        Next
    
    End Sub
    
    0 讨论(0)
  • 2020-12-14 09:15

    Put a panel around the part of the page that you want disabled:

       < asp:Panel ID="pnlPage" runat="server" >
          ...
       < /asp:Panel >
    

    Inside of Page_Load:

       If Not Me.Page.IsPostBack Then
          Me.pnlPage.Enabled = False
       End If
    

    ... or the C# equivalent. :o)

    0 讨论(0)
提交回复
热议问题