Part I of this topic dealt with overriding the basic FindControl method found native to objects inheriting from the System.Web.UI.Control object, which most people find severely lacking for uses other than simple pages/applications.
In this part, I will look at a simple extension from the recursive nature of the custom FindControl implementation that will allow us to find controls by another method: by type. Instead of searching for a single control by it’s string ID, we can scour any controls collection recursively for any controls which are of type X.
public static ArrayList FindControls(Type type, ControlCollection col)
{
ArrayList list = new ArrayList();
foreach(Control c in col)
{
if(c.GetType() == type)
list.Add(c);
else
FindControlsRecursive(c, type, ref list);
}
return list;
}
private static void FindControlsRecursive(Control root, Type type, ref ArrayList list)
{
if(root.Controls.Count != 0)
{
foreach(Control c in root.Controls)
{
if(c.GetType() == type)
list.Add(c);
else if (c.HasControls())
FindControlsRecursive(c, type, ref list);
}
}
}
As you can see, the implementation of the recursive method is slightly different than the previous version for the single control. The only real difference is the presence of a by ref argument which is a data structure (in this case an ArrayList) to keep track of the results found.
Pingback: Leading the Next Inquisition » Blog Archive » Custom FindControl Implementation (C#)
I haven’t tested this yet, but …
I think the FindControlsRecursive() will not find a control of a given type within a control of the same type, for example: a panel within a panel.
Therefore I think the body of the “for” loop should be:
if(c.GetType() == type)
list.Add(c);
if (c.HasControls())
FindControlsRecursive(c, type, ref list);
I omitted the “else” keyword.
Test if you like, or I may (??) return and test it when I have some time.
8Icqoo comment1 ,
i had to change
if(c.HasControls())
to
if (c.HasChildren)
and make the ControlCollection argument type a simple Control[] array for my Winform app.
Great utility method. Thanks!
public static ArrayList FindControls(Control control)
{
ArrayList list = new ArrayList();
FindControlsRecursive(control, ref list);
return list;
}
private static void FindControlsRecursive(Control root, ref ArrayList list)
{
if (root.Controls.Count != 0)
{
foreach (T c in root.Controls)
{
list.Add(c);
FindControlsRecursive(c, ref list);
}
}
}
FindControls(this.groupBox1); // For example
The stupid editor deleted the \ type parameter in lines:
public static ArrayList FindControls(Control control)
FindControlsRecursive(control, ref list);
private static void FindControlsRecursive(Control root, ref ArrayList list)
FindControlsRecursive(c, ref list);
And it deleted the \ from this line:
FindControls(this.groupBox1); // For example
public static ArrayList FindControls (T) (Control control)
FindControlsRecursive (T) (control, ref list);
private static void FindControlsRecursive (T) (Control root, ref ArrayList list)
FindControlsRecursive (T) (c, ref list);
And it deleted the \ from this line:
FindControls ( TextBox) (this.groupBox1); // For example
This crap doesn’t allow this characters:
http://www.fileformat.info/info/unicode/char/003c/index.htm
http://www.fileformat.info/info/unicode/char/3e/index.htm