Click here to Skip to main content
Licence CPOL
First Posted 23 Jul 2007
Views 41,476
Bookmarked 19 times

Enable or disable all controls in the page using simple code

By | 25 Feb 2008 | Article
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)

About the Author

George Zacharia

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberbacm3:11 3 Apr '12  
GeneralMy vote of 5 Pinmemberhemantaboruah0:35 9 Jul '11  
Questionhow to disable them in child page? Pinmemberaditshah_2223:49 2 Dec '10  
GeneralMy vote of 1 Pinmembersystem.sameer.code7:23 7 Jul '09  
GeneralMy vote of 1 Pinmembermahone8:34 15 Jan '09  
GeneralMy vote of 1 PinmemberidriseAbdi5:34 15 Jan '09  
GeneralYour article made it to The Daily WTF PinmemberJustin Perez2:46 26 Feb '08  
Questionone prob... Pinmemberbhaiwaah13:52 7 Sep '07  
GeneralTDWTF Pinmemberunklegwar10:22 23 Jul '07  
GeneralAnother choice PinmemberBruno Renato2:17 23 Jul '07  
GeneralCould be done another way... PinmemberHotcut1:39 23 Jul '07  
GeneralRe: Could be done another way... Pinmemberatarikg1:55 23 Jul '07  
GeneralRe: Could be done another way... PinmemberHotcut1:58 23 Jul '07  
GeneralRe: Could be done another way... Pinmemberatarikg2:15 23 Jul '07  
GeneralRe: Could be done another way... PinmemberLuc Pattyn3:16 23 Jul '07  
GeneralRe: Could be done another way... PinmemberHotcut3:18 23 Jul '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 26 Feb 2008
Article Copyright 2007 by George Zacharia
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid