Click here to Skip to main content
12,452,056 members (26,089 online)
Click here to Skip to main content
Add your own
alternative version

Stats

85.7K views
21 bookmarked
Posted

Enable or disable all controls in the page using simple code

, 25 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

George Zacharia
Web Developer
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Suggestioncode is not working Pin
Valentino_Lokesh5-Mar-13 1:42
memberValentino_Lokesh5-Mar-13 1:42 
GeneralMy vote of 1 Pin
bacm3-Apr-12 3:11
memberbacm3-Apr-12 3:11 
GeneralMy vote of 5 Pin
hemantaboruah9-Jul-11 0:35
memberhemantaboruah9-Jul-11 0:35 
Questionhow to disable them in child page? Pin
aditshah_222-Dec-10 23:49
memberaditshah_222-Dec-10 23:49 
GeneralMy vote of 1 Pin
system.sameer.code7-Jul-09 7:23
membersystem.sameer.code7-Jul-09 7:23 
GeneralMy vote of 1 Pin
mahone15-Jan-09 8:34
membermahone15-Jan-09 8:34 
GeneralMy vote of 1 Pin
idriseAbdi15-Jan-09 5:34
memberidriseAbdi15-Jan-09 5:34 
GeneralYour article made it to The Daily WTF Pin
Justin Perez26-Feb-08 2:46
memberJustin Perez26-Feb-08 2:46 
Questionone prob... Pin
bhaiwaah7-Sep-07 13:52
memberbhaiwaah7-Sep-07 13:52 
GeneralTDWTF Pin
unklegwar23-Jul-07 10:22
memberunklegwar23-Jul-07 10:22 
GeneralAnother choice Pin
Bruno Renato23-Jul-07 2:17
memberBruno Renato23-Jul-07 2:17 
GeneralCould be done another way... Pin
Hotcut23-Jul-07 1:39
memberHotcut23-Jul-07 1:39 
GeneralRe: Could be done another way... Pin
atarikg23-Jul-07 1:55
memberatarikg23-Jul-07 1:55 
GeneralRe: Could be done another way... Pin
Hotcut23-Jul-07 1:58
memberHotcut23-Jul-07 1:58 
GeneralRe: Could be done another way... Pin
atarikg23-Jul-07 2:15
memberatarikg23-Jul-07 2:15 
GeneralRe: Could be done another way... Pin
Luc Pattyn23-Jul-07 3:16
memberLuc Pattyn23-Jul-07 3:16 
GeneralRe: Could be done another way... Pin
Hotcut23-Jul-07 3:18
memberHotcut23-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.

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