在webform中查找控件

我有一个Web内容表单,需要访问内容面板中的控件。 我知道有两种访问控件的方法:

  1. TextBox txt = (TextBox)Page.Controls[0].Controls[3].Controls[48].Controls[6]
  2. 通过编写一个搜索所有控件的递归函数。

还有其他更简单的方法,因为Page.FindControl在这个实例中不起作用。 我问的原因是我感觉像Page对象或Content Panel对象应该有一个方法来查找子控件,但找不到类似的东西。

问题是FindControl()不会遍历某些控制子项,例如模板化控件。 如果您使用的控件存在于模板中,则无法找到它。

所以我们添加了以下扩展方法来处理这个问题。 如果您没有使用3.5或想要避免使用扩展方法,则可以使用这些方法创建通用库。

您现在可以通过编码获得您所追求的控制:

 var button = Page.GetControl("MyButton") as Button; 

扩展方法为您执行递归工作。 希望这可以帮助!

 public static IEnumerable Flatten(this ControlCollection controls) { List list = new List(); controls.Traverse(c => list.Add(c)); return list; } public static IEnumerable Flatten(this ControlCollection controls, Func predicate) { List list = new List(); controls.Traverse(c => { if (predicate(c)) list.Add(c); }); return list; } public static void Traverse(this ControlCollection controls, Action action) { foreach (Control control in controls) { action(control); if (control.HasControls()) { control.Controls.Traverse(action); } } } public static Control GetControl(this Control control, string id) { return control.Controls.Flatten(c => c.ID == id).SingleOrDefault(); } public static IEnumerable GetControls(this Control control) { return control.Controls.Flatten(); } 

我想将GetControls函数更改为通用函数,如下所示:

 public static T GetControl(this Control control, string id) where T:Control { var result = control.Controls.Flatten(c => (c.GetType().IsSubclassOf(typeof(T))) && (c.ID == id)).SingleOrDefault(); if (result == null) return null; return result as T; } 

然后,

 public static Control GetControl(this Control control, string id) { return control.GetControl(id); } 

这样,调用者会调用类似于:

 var button = Page.GetControl