Sponsored Ad

Friday, March 19, 2010

How to Find Control Recursively from Collection of Controls

 

I am going to write a code script to find controls recursively. This is sample function to search for a control in a collection of controls and if it present in the collection of controls just the return the control.

This public method accept the control id to search for and a collection of controls.

and it return control incase of successful search and null if control not present.

 

public static Control FindControlRecursively(string controlID, ControlCollection controls)
       {
           if (controlID == null || controls == null)
               return null;

           foreach (Control c in controls)
           {
               if (c.ID == controlID)
                   return c;

               if (c.HasControls())
               {
                   Control inner = FindControlRecursively(controlID,      c.Controls);
                   if (inner != null)
                       return inner;
               }
           }
           return null;
       }

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers