我的 asp 页面中有 45 个下拉列表。我可以将一些方法应用于所有这些下拉菜单。是否可以将它们转换为下拉列表数组以便于使用?
最佳答案
我会使用递归来查找页面上的所有下拉菜单。基于此post它会是这样的:
public static List<T> FindControls<T>(System.Web.UI.ControlCollection Controls) where T : class
{
List<T> found = new List<T>();
FindControls<T>(Controls, found);
return found;
}
private static void FindControls<T>(System.Web.UI.ControlCollection Controls, List<T> found) where T : class
{
if (Controls != null && Controls.Count > 0)
{
for (int i = 0; i < Controls.Count; i++)
{
if (Controls[i] is T)
{
found.add(Controls[i] as T);
}
else
FindControl<T>(Controls[i].Controls, found);
}
}
}
获得下拉列表后,您可以应用您认为合适的任何方法。
关于asp.net - 在 ASP.NET 中,我可以将一堆下拉列表转换为下拉列表数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5272138/