Nested User Controls - how to best get a reference to an ancestor control

后端 未结 5 1950
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 11:31

I realize that a whole lot of code cannot fit here, but I am asking for general direction or pointer.

I have .NET user controls nested six deep for an interactive gadget

5条回答
  •  悲&欢浪女
    2021-01-23 12:16

    public static class Extension{
     public static Control FindControlRecursive(this Control control, string idToFind)
     {
         Control result;
         if(control != null){
            result = control.FindControl(idToFind);
         }
         if(result != null){ return result; }
         if(result == null && control.Parent != null){
             return control.Parent.FindRecursive(idToFind);
         }
         return null;
      }
    }
    

提交回复
热议问题