2009-01-30 92 views

回答

9

Page.FindControl

如果控制是嵌套的,使用Control.FindControl從父控制。否則,您必須編寫自己的FindControlRecursive

+0

注意:在控件集合中,FindControl方法將返回該名稱控件的不同實例。在ASP.NET中,該控件也將具有唯一的名稱。換句話說,FindControlRecursive將返回該名稱的第一個控件。 – 2009-01-30 04:45:17

1
 private Control FindControlRecursive(Control root, string id) 
     { 
      return root.ID == id 
         ? root 
         : (root.Controls.Cast<Control>() 
          .Select(c => FindControlRecursive(c, id))) 
          .FirstOrDefault(t => t != null); 
     } 
相關問題