Enable or disable all controls in the page using simple code






1.06/5 (26 votes)
Enable or disable all the server controls in a page with a small piece of code

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