Click here to Skip to main content
15,881,757 members
Articles / Web Development / ASP.NET
Article

Enable or disable all controls in the page using simple code

Rate me:
Please Sign up or sign in to vote.
1.06/5 (30 votes)
25 Feb 2008CPOL 117.1K   21   17
Enable or disable all the server controls in a page with a small piece of code
Screenshot - Disable.jpg

Introduction

This article helps to enable or disable all the controls in a page in one step without writing enable or disable for each control in the page. The article also helps users to Clear all the controls in page by one step.

Using the code

ChangeControlStatus(false) ;

The Method ChangeControl status accepts a boolean parameter .The parameter value can be set to True/False. When the Parameter value is false all the Controls are disabled and vice versa.

private void ChangeControlStatus(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 ImageButton)

            ((ImageButton)ctrl).Enabled = status;

          else if (ctrl is CheckBox)

            ((CheckBox)ctrl).Enabled = status;

          else if (ctrl is DropDownList)

            ((DropDownList)ctrl).Enabled = status; 
 
       else if (ctrl is HyperLink)

        ((HyperLink)ctrl).Enabled = status; 

 }
 private void ClearControls()
    {
        foreach(Control c in Page.Controls)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = string.Empty;
                }
            }
        }
    } 

Calling the Method

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

ChangeControlStatus(false);
}

History

Created on 23/07/2007 by George Zacharia .Modified on 24/07/2007

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestioncode is not working Pin
Valentino_Lokesh5-Mar-13 1:42
Valentino_Lokesh5-Mar-13 1:42 
XML
List<WebControl> wcs = new List<WebControl>();
GetControlList<WebControl>(Page.Controls, wcs)
foreach (WebControl childControl in wcs)
{
     if(childControl.CssClass == "required") {
     // do the
      }
}

private void GetControlList<T>(ControlCollection controlCollection, ref List<T> resultCollection)
where T : Control
{
    foreach (Control control in controlCollection)
    {
        //if (control.GetType() == typeof(T))
        if (control is T) // This is cleaner
            resultCollection.Add((T)control);

        if (control.HasControls())
            GetControlList(control.Controls, ref resultCollection);
    }
}

Thanks

GeneralMy vote of 1 Pin
bacm3-Apr-12 3:11
bacm3-Apr-12 3:11 
GeneralMy vote of 5 Pin
hemantaboruah9-Jul-11 0:35
hemantaboruah9-Jul-11 0:35 
Questionhow to disable them in child page? Pin
aditshah_222-Dec-10 23:49
aditshah_222-Dec-10 23:49 
GeneralMy vote of 1 Pin
system.sameer.code7-Jul-09 7:23
system.sameer.code7-Jul-09 7:23 
GeneralMy vote of 1 Pin
mahone15-Jan-09 8:34
mahone15-Jan-09 8:34 
GeneralMy vote of 1 Pin
idriseAbdi15-Jan-09 5:34
idriseAbdi15-Jan-09 5:34 
GeneralYour article made it to The Daily WTF Pin
Justin Perez26-Feb-08 2:46
Justin Perez26-Feb-08 2:46 
Questionone prob... Pin
bhaiwaah7-Sep-07 13:52
bhaiwaah7-Sep-07 13:52 
GeneralTDWTF Pin
unklegwar23-Jul-07 10:22
unklegwar23-Jul-07 10:22 
GeneralAnother choice Pin
Bruno Renato23-Jul-07 2:17
Bruno Renato23-Jul-07 2:17 
GeneralCould be done another way... Pin
Hotcut23-Jul-07 1:39
Hotcut23-Jul-07 1:39 
GeneralRe: Could be done another way... Pin
Tarik Guney23-Jul-07 1:55
Tarik Guney23-Jul-07 1:55 
GeneralRe: Could be done another way... Pin
Hotcut23-Jul-07 1:58
Hotcut23-Jul-07 1:58 
GeneralRe: Could be done another way... Pin
Tarik Guney23-Jul-07 2:15
Tarik Guney23-Jul-07 2:15 
GeneralRe: Could be done another way... Pin
Luc Pattyn23-Jul-07 3:16
sitebuilderLuc Pattyn23-Jul-07 3:16 
GeneralRe: Could be done another way... PinPopular
Hotcut23-Jul-07 3:18
Hotcut23-Jul-07 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.