在ASP.NET中,我可以将一组下拉列表转换为一组下拉列表吗?

在ASP.NET中,我可以将一组下拉列表转换为一组下拉列表吗?,asp.net,drop-down-menu,Asp.net,Drop Down Menu,我的asp页面中有45个下拉列表。有一些方法可以应用于所有这些下拉列表。是否可以将它们转换为一组下拉列表以便于使用?我会使用递归查找页面上的所有下拉列表。基于此,它将类似于: public static List<T> FindControls<T>(System.Web.UI.ControlCollection Controls) where T : class { List<T> found = new List<T>(); FindC

我的asp页面中有45个下拉列表。有一些方法可以应用于所有这些下拉列表。是否可以将它们转换为一组下拉列表以便于使用?

我会使用递归查找页面上的所有下拉列表。基于此,它将类似于:

    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);
              }
         }

       }
公共静态列表FindControl(System.Web.UI.ControlCollection控件),其中T:class
{
找到的列表=新列表();
FindControl(控件,已找到);
发现退货;
}
私有静态void FindControls(System.Web.UI.ControlCollection控件,找到列表),其中T:class
{
if(Controls!=null&&Controls.Count>0)
{
for(int i=0;i

一旦您有了下拉列表,您就可以应用任何您认为合适的方法。

使用Linq to Objects、扩展方法和泛型,我们可以使其非常简洁,因此:

方法调用以获取所有下拉列表
var DropDowns=FindAllControlsByType(MyBaseControlArray);
查找控件方法
公共静态IEnumerable FindAllControlsByType(IEnumerable MyCollection),其中T:class
{
返回MyCollection.Cast()子体(d=>d.Controls.Cast())。其中(l=>l.GetType().Equals(typeof(T));
}
子体扩展法
静态公共类LinqExtensions
{
静态公共IEnumerable子代(此IEnumerable源,
Func(按)
{
foreach(源中的T值)
{
收益回报值;
foreach(子代中的T子代(值)。子代(子代))
{
退换子女;
}
}
}
}
编辑 我一直在考虑让这一切变得更加通用,所以这里有一个完全通用的解决方案,它将从指定的起点遍历对象图,提取给定类型的所有元素

public static class Utils
{
    public static IEnumerable<IEnumerable<T>> GetCollections<T>(object obj)
    {
        if (obj == null) throw new ArgumentNullException("obj");
        var type = obj.GetType();
        var res = new List<IEnumerable<T>>();
        foreach (var prop in type.GetProperties())
        {
            // is IEnumerable<T>?
            if (typeof(IEnumerable<T>).IsAssignableFrom(prop.PropertyType))
            {
                var get = prop.GetGetMethod();
                if (!get.IsStatic && get.GetParameters().Length == 0) // skip indexed & static
                {
                    var collection = (IEnumerable<T>)get.Invoke(obj, null);
                    if (collection != null) res.Add(collection);
                }
            }
        }
        return res;
    }

    public static IEnumerable<V> FindAllControlsByType<V, T>(V MyCollection) where T : class
    {
        return Utils.GetCollections<V>(MyCollection).Descendants(d => d).Where(l => typeof(T).IsAssignableFrom(l.GetType()));
    }
}

static public class LinqExtensions
{
    static public IEnumerable<T> Descendants<T>(this IEnumerable<IEnumerable<T>> source,
                                                Func<IEnumerable<IEnumerable<T>>, IEnumerable<IEnumerable<T>>> DescendBy)
    {
        foreach (IEnumerable<T> collection in source)
        {
            foreach (T value in collection)
            {
                yield return value;

                foreach (T child in DescendBy(Utils.GetCollections<T>(value)).Descendants<T>(DescendBy))
                {
                    yield return child;
                }
            }
        }
    }
}
公共静态类Utils
{
公共静态IEnumerable GetCollections(对象obj)
{
如果(obj==null)抛出新的ArgumentNullException(“obj”);
var type=obj.GetType();
var res=新列表();
foreach(type.GetProperties()中的var prop)
{
//是可数的吗?
if(typeof(IEnumerable).IsAssignableFrom(prop.PropertyType))
{
var get=prop.getMethod();
如果(!get.IsStatic&&get.GetParameters().Length==0)//跳过索引和静态
{
var collection=(IEnumerable)get.Invoke(obj,null);
if(collection!=null)res.Add(collection);
}
}
}
返回res;
}
公共静态IEnumerable FindAllControlsByType(V MyCollection),其中T:class
{
返回Utils.GetCollections(MyCollection).substands(d=>d).Where(l=>typeof(T).IsAssignableFrom(l.GetType());
}
}
静态公共类LinqExtensions
{
静态公共IEnumerable子代(此IEnumerable源,
Func(按)
{
foreach(源中的IEnumerable集合)
{
foreach(集合中的T值)
{
收益回报值;
foreach(子代中的T子代(Utils.GetCollections(value)).subjects(子代))
{
退换子女;
}
}
}
}
}
我们可以称之为:

var DropDowns = Utils.FindAllControlsByType<Control, DropDownList>(BaseControl);
var DropDowns=Utils.FindAllControlsByType(BaseControl);

基本上,这两种类型是基类和要提取的特定子类。您将注意到,该过程标识基类的每个实例中包含的所有基类集合。这意味着我们不依赖于
控件作为集合,可以在其他结构中使用它。欢迎任何其他优化。

什么样的方法?将数据绑定到它们,然后在页面上呈现它们?是的,其中一个是将数据绑定到它们,另一个是从它们中检索选定的数据以插入数据库等。如果在一个页面上有45个下拉列表,则是吗?哇。。。这是在某种网格中吗?不,它不在网格上,它在页面上。如果我们有一个控件集合,控件为T,后面是一个包含控件T的分组控件,会发生什么?我们声明List found,添加我们对T的控制,调用self,创建一个新的本地列表,添加T的子控件,返回本地列表并将其分配给初始列表。。。我们不会失去对t的第一控制吗?众所周知,我不善于在头脑中运行代码,所以我可能遗漏了一些明显的东西,我完全愿意被纠正。@Lazarus-你说得对,Lazarus。我已经相应地修改了代码。很抱歉,我不理解linq,我想这对很多其他人都很有用……你可能是对的(在这两种说法上),这没关系,因为我不是真的为你写的,而是为我自己写的。这是一次学习活动。
static public class LinqExtensions
{
    static public IEnumerable<T> Descendants<T>(this IEnumerable<T> source, 
                                                Func<T, IEnumerable<T>> DescendBy)
    {
        foreach (T value in source)
        {
            yield return value;

            foreach (T child in DescendBy(value).Descendants<T>(DescendBy))
            {
                yield return child;
            }
        }
    }
}
public static class Utils
{
    public static IEnumerable<IEnumerable<T>> GetCollections<T>(object obj)
    {
        if (obj == null) throw new ArgumentNullException("obj");
        var type = obj.GetType();
        var res = new List<IEnumerable<T>>();
        foreach (var prop in type.GetProperties())
        {
            // is IEnumerable<T>?
            if (typeof(IEnumerable<T>).IsAssignableFrom(prop.PropertyType))
            {
                var get = prop.GetGetMethod();
                if (!get.IsStatic && get.GetParameters().Length == 0) // skip indexed & static
                {
                    var collection = (IEnumerable<T>)get.Invoke(obj, null);
                    if (collection != null) res.Add(collection);
                }
            }
        }
        return res;
    }

    public static IEnumerable<V> FindAllControlsByType<V, T>(V MyCollection) where T : class
    {
        return Utils.GetCollections<V>(MyCollection).Descendants(d => d).Where(l => typeof(T).IsAssignableFrom(l.GetType()));
    }
}

static public class LinqExtensions
{
    static public IEnumerable<T> Descendants<T>(this IEnumerable<IEnumerable<T>> source,
                                                Func<IEnumerable<IEnumerable<T>>, IEnumerable<IEnumerable<T>>> DescendBy)
    {
        foreach (IEnumerable<T> collection in source)
        {
            foreach (T value in collection)
            {
                yield return value;

                foreach (T child in DescendBy(Utils.GetCollections<T>(value)).Descendants<T>(DescendBy))
                {
                    yield return child;
                }
            }
        }
    }
}
var DropDowns = Utils.FindAllControlsByType<Control, DropDownList>(BaseControl);