Пример #1
0
        /// <summary>
        /// Register a data control to give it Dynamic Data behavior
        /// </summary>
        /// <param name="setSelectionFromUrl">When true, if a primary key is found in the route values
        ///     (typically on the query string), it will get be set as the selected item. This only applies
        ///     to list controls.</param>
        public void RegisterControl(Control control, bool setSelectionFromUrl)
        {
            //
            if (DesignMode)
            {
                return;
            }

            IDataBoundControlInterface dataBoundControl = DataControlHelper.GetDataBoundControl(control, true /*failIfNotFound*/);

            // If we can't get an associated IDynamicDataSource, don't do anything
            IDynamicDataSource dataSource = dataBoundControl.DataSourceObject as IDynamicDataSource;

            if (dataSource == null)
            {
                return;
            }
            // If we can't get a MetaTable from the data source, don't do anything
            MetaTable table = MetaTableHelper.GetTableWithFullFallback(dataSource, Context.ToWrapper());

            // Save the datasource so we can process its parameters in OnLoad. The value we set is irrelevant
            _dataSources[dataSource] = null;

            ((INamingContainer)control).SetMetaTable(table);

            BaseDataBoundControl baseDataBoundControl = control as BaseDataBoundControl;

            if (baseDataBoundControl != null)
            {
                EnablePersistedSelection(baseDataBoundControl, table);
            }

            RegisterControlInternal(dataBoundControl, dataSource, table, setSelectionFromUrl, Page.IsPostBack);
        }
Пример #2
0
        internal static IDynamicDataSource FindDataSourceControl(Control current)
        {
            for (; ; current = current.NamingContainer)
            {
                // Don't look further than the Page, or if the control is not added to a page hierarchy
                if (current == null || current is Page)
                {
                    return(null);
                }

                IDataBoundControlInterface dataBoundControl = GetDataBoundControl(current, false /*failIfNotFound*/);

                // Not a data control: continue searching
                if (dataBoundControl == null)
                {
                    continue;
                }
                // Return its DynamicDataSource
                return(dataBoundControl.DataSourceObject as IDynamicDataSource);
            }
        }
Пример #3
0
        internal static IDataBoundControlInterface GetDataBoundControl(Control control, bool failIfNotFound)
        {
            if (control is IDataBoundControlInterface)
            {
                return((IDataBoundControlInterface)control);
            }
            IDataBoundControlInterface dataBoundControl = null;

            if (control is Repeater)
            {
                dataBoundControl = GetControlAdapter(control);
            }

            if (dataBoundControl == null && failIfNotFound)
            {
                throw new Exception(String.Format(
                                        CultureInfo.CurrentCulture,
                                        DynamicDataResources.DynamicDataManager_UnsupportedControl,
                                        control.GetType()));
            }

            return(dataBoundControl);
        }
Пример #4
0
        internal void RegisterControlInternal(IDataBoundControlInterface dataBoundControl, IDynamicDataSource dataSource, IMetaTable table, bool setSelectionFromUrl, bool isPostBack) {
            // Set the auto field generator (for controls that support it - GridView and DetailsView)
            IFieldControl fieldControl = dataBoundControl as IFieldControl;
            if (fieldControl != null) {
                fieldControl.FieldsGenerator = new DefaultAutoFieldGenerator(table);
            }
            var linqDataSource = dataSource as LinqDataSource;
            var entityDataSource = dataSource as EntityDataSource;
            // If the context type is not set, we need to set it
            if (dataSource.ContextType == null) {
                dataSource.ContextType = table.DataContextType;

                // If it's a LinqDataSurce, register for ContextCreating so the context gets created using the correct ctor
                // Ideally, this would work with other datasource, but we just don't have the right abstraction
                if (linqDataSource != null) {
                    linqDataSource.ContextCreating += delegate(object sender, LinqDataSourceContextEventArgs e) {
                        e.ObjectInstance = table.CreateContext();
                    };
                }

                if (entityDataSource != null) {
                    entityDataSource.ContextCreating += delegate(object sender, EntityDataSourceContextCreatingEventArgs e) {
                        e.Context = (ObjectContext)table.CreateContext();
                    };
                }
            }

            // If the datasource doesn't have an EntitySetName (aka TableName), set it from the meta table
            if (String.IsNullOrEmpty(dataSource.EntitySetName)) {
                dataSource.EntitySetName = table.DataContextPropertyName;
            }

            // If there is no Where clause, turn on auto generate
            if (String.IsNullOrEmpty(dataSource.Where)) {
                dataSource.AutoGenerateWhereClause = true;
            }

            // If it's a LinqDataSource and the flag is set, pre load the foreign keys
            if (AutoLoadForeignKeys && linqDataSource != null) {
                linqDataSource.LoadWithForeignKeys(table.EntityType);
            }

            if (!isPostBack) {
                if (table.HasPrimaryKey) {
                    dataBoundControl.DataKeyNames = table.PrimaryKeyNames;

                    // Set the virtual selection from the URL if needed
                    var dataKeySelector = dataBoundControl as IPersistedSelector;
                    if (dataKeySelector != null && setSelectionFromUrl) {
                        DataKey dataKey = table.GetDataKeyFromRoute();
                        if (dataKey != null) {
                            dataKeySelector.DataKey = dataKey;
                        }
                    }
                }
            }
        }
Пример #5
0
        internal void RegisterControlInternal(IDataBoundControlInterface dataBoundControl, IDynamicDataSource dataSource, IMetaTable table, bool setSelectionFromUrl, bool isPostBack)
        {
            // Set the auto field generator (for controls that support it - GridView and DetailsView)
            IFieldControl fieldControl = dataBoundControl as IFieldControl;

            if (fieldControl != null)
            {
                fieldControl.FieldsGenerator = new DefaultAutoFieldGenerator(table);
            }
            var linqDataSource   = dataSource as LinqDataSource;
            var entityDataSource = dataSource as EntityDataSource;

            // If the context type is not set, we need to set it
            if (dataSource.ContextType == null)
            {
                dataSource.ContextType = table.DataContextType;

                // If it's a LinqDataSurce, register for ContextCreating so the context gets created using the correct ctor
                // Ideally, this would work with other datasource, but we just don't have the right abstraction
                if (linqDataSource != null)
                {
                    linqDataSource.ContextCreating += delegate(object sender, LinqDataSourceContextEventArgs e) {
                        e.ObjectInstance = table.CreateContext();
                    };
                }

                if (entityDataSource != null)
                {
                    entityDataSource.ContextCreating += delegate(object sender, EntityDataSourceContextCreatingEventArgs e) {
                        e.Context = (ObjectContext)table.CreateContext();
                    };
                }
            }

            // If the datasource doesn't have an EntitySetName (aka TableName), set it from the meta table
            if (String.IsNullOrEmpty(dataSource.EntitySetName))
            {
                dataSource.EntitySetName = table.DataContextPropertyName;
            }

            // If there is no Where clause, turn on auto generate
            if (String.IsNullOrEmpty(dataSource.Where))
            {
                dataSource.AutoGenerateWhereClause = true;
            }

            // If it's a LinqDataSource and the flag is set, pre load the foreign keys
            if (AutoLoadForeignKeys && linqDataSource != null)
            {
                linqDataSource.LoadWithForeignKeys(table.EntityType);
            }

            if (!isPostBack)
            {
                if (table.HasPrimaryKey)
                {
                    dataBoundControl.DataKeyNames = table.PrimaryKeyNames;

                    // Set the virtual selection from the URL if needed
                    var dataKeySelector = dataBoundControl as IPersistedSelector;
                    if (dataKeySelector != null && setSelectionFromUrl)
                    {
                        DataKey dataKey = table.GetDataKeyFromRoute();
                        if (dataKey != null)
                        {
                            dataKeySelector.DataKey = dataKey;
                        }
                    }
                }
            }
        }