ASP.NET动态数据过滤

时间:2011-07-29 19:25:48

标签: asp.net dynamic-data asp.net-dynamic-data

我想将ASP.NET动态数据4.0与EF POCO ObjectContext一起用作系统管理员的简单实体管理控制台(我正在考虑放弃这项技术......)。

我已经插入/更新了。

我已经想出如何为额外的数据类型添加过滤器(因为内置的数据类型是无用的)。我无法弄清楚如何使QueryableFilterRepeater将我的列公开为过滤。我无法将FilterUIHints添加到所有适用的类型,因此这不是一个可接受的解决方案。我确实想利用内置的Entity Framework元数据模型提供程序。我不想写自己的。问题似乎归结为QueryableFilterIterator正在调用MetaTable.GetFilteredColumns()并且它只返回bool / int / DateTime列(这是无用的)。

我查看了ASP.NET动态数据过滤(http://dynamicdatafiltering.codeplex.com/),但似乎没有为4.0维护。

我想我有两个问题:

  1. 有没有办法做到这一点(让我的专栏可以过滤?)
  2. 动态数据是否真的适合在现实世界中使用?

1 个答案:

答案 0 :(得分:1)

沮丧并采取了直接的路线。如果没有更好的解决方案......那么,微软在这方面做得很差:

public class QueryableFilterRepeater : System.Web.DynamicData.QueryableFilterRepeater
    {
        private static readonly FieldInfo FiltersField = typeof(System.Web.DynamicData.QueryableFilterRepeater).GetField("_filters", BindingFlags.Instance | BindingFlags.NonPublic);
        private static readonly FieldInfo DataSourceField = typeof(System.Web.DynamicData.QueryableFilterRepeater).GetField("_dataSource", BindingFlags.Instance | BindingFlags.NonPublic);
        private static readonly MethodInfo FilterInitializeMethod = typeof(DynamicFilter).GetMethod("Initialize", BindingFlags.Instance | BindingFlags.NonPublic);
        private static readonly PropertyInfo FilterContextProperty = typeof(DynamicFilter).GetProperty("Context", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
        private static readonly MethodInfo PageInitCompleteMethod = typeof(System.Web.DynamicData.QueryableFilterRepeater).GetMethod("Page_InitComplete", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
        private static readonly EventInfo InitCompleteEvent = typeof(Page).GetEvent("InitComplete");

        private readonly List<DynamicFilter> filters;
        private int filterCount;
        private bool initialized;

        public IEnumerable<DynamicFilter> Filters
        {
            get { return filters; }
        }

        public QueryableFilterRepeater()
        {
            filters = (List<DynamicFilter>)FiltersField.GetValue(this);
        }

        private IQueryableDataSource DataSource
        {
            get { return DataSourceField.GetValue(this) as IQueryableDataSource; }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            InitCompleteEvent.RemoveEventHandler(Page, Delegate.CreateDelegate(typeof(EventHandler), this, PageInitCompleteMethod));

            Page.InitComplete += new EventHandler(Page_InitComplete);
        }

        protected override void OnLoad(EventArgs e)
        {
            filters.Select((f, i) => new { f, i }).Where(x => x.i > filterCount - 1).ToList().ForEach(x => filters.Remove(x.f));
            Controls.OfType<Control>().Select((c, i) => new { c, i }).Where(x => x.i > filterCount - 1).ToList().ForEach(x => Controls.Remove(x.c));
            base.OnLoad(e);
        }

        private void Page_InitComplete(object sender, EventArgs e)
        {
            if (!initialized)
            {
                Controls.Clear();
                filters.Clear();

                MetaTable metaTable = DataSource.GetMetaTable();
                int num = 0;
                foreach (MetaColumn column in metaTable.Columns)
                {
                    string filterUIHint = GetFilterUIHint(column);
                    if (filterUIHint == null) continue;

                    var filterRepeaterItem = new FilterRepeaterItem();
                    filterRepeaterItem.DataItemIndex = num;
                    filterRepeaterItem.DisplayIndex = num;
                    FilterRepeaterItem container = filterRepeaterItem;
                    num++;
                    ItemTemplate.InstantiateIn(container);
                    Controls.Add(container);
                    var dynamicFilter = container.FindControl(DynamicFilterContainerId) as DynamicFilter;
                    if (dynamicFilter == null)
                    {
                        throw new InvalidOperationException();
                    }
                    FilterContextProperty.SetValue(dynamicFilter, new HttpContextWrapper(Context), null);
                    dynamicFilter.DataField = column.Name;
                    container.DataItem = column;
                    container.DataBind();
                    container.DataItem = null;

                    dynamicFilter.FilterUIHint = filterUIHint;
                    filters.Add(dynamicFilter);
                    filterCount++;
                }
                filters.ForEach(f => FilterInitializeMethod.Invoke(f, new[] { DataSource }));
                initialized = true;
            }
        }

        private string GetFilterUIHint(MetaColumn column)
        {
            if (GetUnderlyingType(column.ColumnType) == typeof(string))
            {
                return "String";
            }
            if (GetUnderlyingType(column.ColumnType) == typeof(bool))
            {
                return "Boolean";
            }
            if (GetUnderlyingType(column.ColumnType).IsEnum)
            {
                return "Enumeration";
            }
            if (GetUnderlyingType(column.ColumnType) == typeof(DateTime))
            {
                return "DateTime";
            }
            if (column is MetaForeignKeyColumn)
            {
                return "ForeignKey";
            }
            if (column is MetaChildrenColumn)
            {
                return "Children";
            }

            return null;
        }

        private Type GetUnderlyingType(Type type)
        {
            return Nullable.GetUnderlyingType(type) ?? type;
        }

        // Nested Types

        #region Nested type: FilterRepeaterItem

        private class FilterRepeaterItem : Control, IDataItemContainer
        {
            // Properties

            #region IDataItemContainer Members

            public object DataItem { get; internal set; }
            public int DataItemIndex { get; internal set; }
            public int DisplayIndex { get; internal set; }

            #endregion
        }

        #endregion
    }