Custom FindControl Implementation (C#) — Part II

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.

Advertisements

7 Comments

Filed under ASP.NET, C#, Programming

7 responses to “Custom FindControl Implementation (C#) — Part II

  1. Pingback: Leading the Next Inquisition » Blog Archive » Custom FindControl Implementation (C#)

  2. BillT

    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.

  3. joe t.

    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!

    • Blumster

      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

  4. Blumster

    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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s