locked
Two ways to find your control RRS feed

  • Question

  • User1270303073 posted

    Hi,


    I would want to introduce two static methods which are used in my portal framework: one to find control by moving up on the control hierarchy and second one is moving down.

     

    1    public static Control FindControlRecursiveDown(Control parent, string controlId)
    2    		{
    3    			Control ctl;
    4    
    5    			foreach(Control obj in parent.Controls)
    6    			{
    7    				if (obj.ID == controlId)
    8    					return obj;
    9    				
    10   				if (obj.HasControls())
    11   				{
    12   					ctl = FindControlRecursiveDown(obj, controlId);
    13   					if (ctl != null)
    14   						return ctl;
    15   				}
    16   			}
    17   
    18   			return null;
    19   		}
    20   
    21   		public static Control FindControlRecursiveUp(Control parent, string controlId)
    22   		{
    23   			Control ctl;
    24   
    25   			ctl = parent.FindControl(controlId);
    26   			if (ctl == null)
    27   			{
    28   				parent = parent.Parent;
    29   				if (parent != null)
    30   					return FindControlRecursiveUp(parent, controlId);
    31   			}
    32   
    33   			return ctl;
    34   		}
    

    It's very easy to understand.

     

    Regards, 

    Friday, October 5, 2007 5:39 AM

Answers

  • User1270303073 posted
    Instead of using HasControls you can use obj.Controls.Count > 0 
     
    1    public static Control FindControlRecursiveDown(Control parent, string controlId)
    2 {
    3 Control ctl;
    4
    5 foreach(Control obj in parent.Controls)
    6 {
    7 if (obj.ID == controlId)
    8 return obj;
    9
    10 if (obj.Controls.Count > 0)
    11 {
    12 ctl = FindControlRecursiveDown(obj, controlId);
    13 if (ctl != null)
    14 return ctl;
    15 }
    16 }
    17
    18 return null;
    19 }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 10, 2007 10:48 PM

All replies

  • User-1367805312 posted

    Heres the ASP.NET VB version of atomics code

     

     Public Shared Function FindControlRecursiveDown(ByVal parent As Control, ByVal controlId As String) As Control
         Dim ctl As Control
        
         For Each obj As Control In parent.Controls
             If obj.ID = controlId Then
               Return obj
            End If
            
             If obj.HasControls() Then
                 ctl = FindControlRecursiveDown(obj, controlId)
                 If ctl IsNot Nothing Then
                     Return ctl
                 End If
             End If
         Next
        
         Return Nothing
     End Function
    
     Public Shared Function FindControlRecursiveUp(ByVal parent As Control, ByVal controlId As String) As Control
         Dim ctl As Control
        
         ctl = parent.FindControl(controlId)
         If ctl Is Nothing Then
             parent = parent.Parent
             If parent IsNot Nothing Then
                 Return FindControlRecursiveUp(parent, controlId)
             End If
         End If
        
         Return ctl
     End Function
     
     
    Friday, October 5, 2007 2:48 PM
  • User1270303073 posted

    Yes the VB version works well

    Wednesday, October 10, 2007 10:44 PM
  • User1270303073 posted
    Instead of using HasControls you can use obj.Controls.Count > 0 
     
    1    public static Control FindControlRecursiveDown(Control parent, string controlId)
    2 {
    3 Control ctl;
    4
    5 foreach(Control obj in parent.Controls)
    6 {
    7 if (obj.ID == controlId)
    8 return obj;
    9
    10 if (obj.Controls.Count > 0)
    11 {
    12 ctl = FindControlRecursiveDown(obj, controlId);
    13 if (ctl != null)
    14 return ctl;
    15 }
    16 }
    17
    18 return null;
    19 }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 10, 2007 10:48 PM
  • User-1367805312 posted

     Thought I'd supply the VB code again [:D]

      

     Public Shared Function FindControlRecursiveDown(ByVal parent As Control, ByVal controlId As String) As Control
         Dim ctl As Control
        
         For Each obj As Control In parent.Controls
             If obj.ID = controlId Then
                 Return obj
             End If
            
             If obj.Controls.Count > 0 Then
                 ctl = FindControlRecursiveDown(obj, controlId)
                 If ctl IsNot Nothing Then
                     Return ctl
                 End If
             End If
         Next
        
         Return Nothing
     End Function
     

     

    Thursday, October 11, 2007 1:15 AM