The Infamous "Clear All Controls" , Generics Style

In the ASP.NET world, its invaluable to be able to clear all controls on a form, or disable all controls, or even reset the controls to a default state. I am going to show you an awesome way to do it, Generics Style.

We will be building this using a method that takes in a Generic Type of Control, and a

///


/// Excellent Method to abstract and add into the Helper classes : Using Generics
///

///

private void ManageControls(Control parent, Func del) where Gen : Control
{


foreach (Gen c in parent.Controls)
{

if (c is Gen)
{
del.Invoke(c);


if (((Control)c).HasControls()) ManageControls(((Control)c), del);

}
}
}

ManageControls(parent, x => x.Items.Clear());
ManageControls(parent, x => x.Text = "");

Comments

Popular posts from this blog