Exemplo n.º 1
0
        private object[] GetDataSourceControls(object instance, IContainer container)
        {
            ComponentCollection components  = container.Components;
            ArrayList           dataSources = new ArrayList();

            ICollection controls = null;

            if (instance is Array)
            {
                controls = (Array)instance;
            }
            else
            {
                controls = new IBusinessObjectBoundWebControl[] { (IBusinessObjectBoundWebControl)instance }
            };

            for (int idxComponents = 0; idxComponents < components.Count; idxComponents++)
            {
                IComponent component = (IComponent)components[idxComponents];
                IBusinessObjectDataSourceControl dataSource = component as IBusinessObjectDataSourceControl;
                if (dataSource != null && !string.IsNullOrEmpty(dataSource.ID))
                {
                    bool hasSelfReference = false;
                    foreach (IBusinessObjectBoundWebControl control in controls)
                    {
                        if (dataSource == control)
                        {
                            hasSelfReference = true;
                            break;
                        }
                    }
                    if (!hasSelfReference)
                    {
                        dataSources.Add(dataSource.ID);
                    }
                }
            }

            dataSources.Sort(Comparer.Default);
            return(dataSources.ToArray());
        }
        /// <summary> Uses the value of <see cref="DataSourceControl"/> to set <see cref="DataSource"/>. </summary>
        /// <remarks>
        ///   Due to <b>Design Mode</b> behavior of Visual Studio .NET the <see cref="IComponent.Site"/> property is
        ///   utilized for finding the data source during <b>Design Mode</b>.
        /// </remarks>
        /// <exception cref="HttpException">
        ///   Thrown if the <see cref="DataSourceControl"/> is not <see langword="null "/> and could not be evaluated
        ///   to a valid <see cref="IBusinessObjectDataSourceControl"/> during <b>Run Time</b>.
        /// </exception>
        public void EnsureDataSource()
        {
            if (_dataSourceChanged)
            {
                // set _dataSource from ID in _dataSourceControl
                if (string.IsNullOrEmpty(_dataSourceControl))
                {
                    SetDataSource(null);
                }
                else
                {
                    bool isDesignMode = ControlHelper.IsDesignMode(_control);

                    Control namingContainer = _control.NamingContainer;
                    if (namingContainer == null)
                    {
                        if (!isDesignMode)
                        {
                            throw new HttpException(string.Format("Cannot evaluate data source because control {0} has no naming container.", _control.ID));
                        }

                        //  HACK: Designmode Naming container
                        //  Not completely sure that Components[0] will always be the naming container.
                        if (_control.Site.Container.Components.Count > 0)
                        {
                            namingContainer = (_control.Site.Container.Components[0]) as Control;
                        }
                        else
                        {
                            return;
                        }
                    }

                    Control control = ControlHelper.FindControl(namingContainer, _dataSourceControl);
                    if (control == null)
                    {
                        if (!isDesignMode)
                        {
                            throw new HttpException(string.Format("Unable to find control id '{0}' referenced by the DataSourceControl property of '{1}'.", _dataSourceControl, _control.ID));
                        }

                        foreach (IComponent component in namingContainer.Site.Container.Components)
                        {
                            if (component is IBusinessObjectDataSourceControl &&
                                component is Control &&
                                ((Control)component).ID == _dataSourceControl)
                            {
                                control = (Control)component;
                                break;
                            }
                        }

                        if (control == null)
                        {
                            SetDataSource(null);
                            _dataSourceChanged = true;
                            return;
                        }
                    }

                    IBusinessObjectDataSourceControl dataSource = control as IBusinessObjectDataSourceControl;
                    if (dataSource == null)
                    {
                        throw new HttpException(string.Format("The control with the id '{0}' referenced by the DataSourceControl property of '{1}' does not identify a control of type '{2}'.", _dataSourceControl, _control.ID, typeof(IBusinessObjectDataSourceControl)));
                    }

                    SetDataSource(dataSource);
                }

                _dataSourceChanged = false;
            }
        }