Monday, April 13, 2009

How to disable all the ASP.NET input controls within a container control

The following method will recursively disable all the input controls within a container control such as a User Control, PlaceHolder, Panel etc.:

public void DisableInputControls(Control container)
{
 foreach (Control control in container.Controls)
 {
  if (control is TextBox
   || control is DropDownList
   || control is RadioButtonList
   || control is RadioButton
   || control is CheckBoxList
   || control is CheckBox
   || control is Button)
  { 
   (control as WebControl).Enabled = false;
  }
  
  if (control.Controls.Count > 0)
   DisableInputControls(control);
 } 
}

Example usage:


DisableInputControls(MyPlaceHolder);