Tuesday, December 9, 2014

How to disable all controls on the Page

How to disable all controls on a page.

You can add all different controls in the code below to disable and enable them. This fill find all controls on the page and will disable or enable it based on value being passed to it.
public void DisablePageControls(bool status)
        {

            foreach (Control c in Page.Controls)
            {
                foreach (Control ctrl in c.Controls)
                {

                    if (ctrl is TextBox)
                        ((TextBox)ctrl).Enabled = status;
                    else if (ctrl is Button)
                        ((Button)ctrl).Enabled = status;
                    else if (ctrl is RadioButton)
                        ((RadioButton)ctrl).Enabled = status;
                    else if (ctrl is RadioButtonList)
                        ((RadioButtonList)ctrl).Enabled = status;
                    else if (ctrl is ImageButton)
                        ((ImageButton)ctrl).Enabled = status;
                    else if (ctrl is CheckBox)
                        ((CheckBox)ctrl).Enabled = status;
                    else if (ctrl is CheckBoxList)
                        ((CheckBoxList)ctrl).Enabled = status;
                    else if (ctrl is DropDownList)
                        ((DropDownList)ctrl).Enabled = status;
                    else if (ctrl is HyperLink)
                        ((HyperLink)ctrl).Enabled = status;
                }
            }

        }

DisablePageControls(false); // To enable
DisablePageControls(true); // To disable


No comments:

Post a Comment