コード例 #1
0
        protected virtual void OnCallingDataMethods(CallingDataMethodsEventArgs e)
        {
            CallingDataMethodsEventHandler handler = Events[EventCallingDataMethods] as CallingDataMethodsEventHandler;

            if (handler != null)
            {
                handler(_owner.DataControl, e);
            }
        }
コード例 #2
0
        /// <devdoc>
        /// Gets the IDataSource that this control is bound to, if any.
        /// Because this method can be called directly by derived classes, it's virtual so data can be retrieved
        /// from data sources that don't live on the page.
        /// </devdoc>
        protected virtual IDataSource GetDataSource()
        {
            if (!DesignMode && IsUsingModelBinders)
            {
                //Let the developer choose a custom ModelDataSource.
                CreatingModelDataSourceEventArgs e = new CreatingModelDataSourceEventArgs();
                OnCreatingModelDataSource(e);
                if (e.ModelDataSource != null)
                {
                    ModelDataSource = e.ModelDataSource;
                }

                //Update the properties of ModelDataSource so that it's ready for data-binding.
                UpdateModelDataSourceProperties(ModelDataSource);

                CallingDataMethodsEventHandler handler = Events[EventCallingDataMethods] as CallingDataMethodsEventHandler;
                if (handler != null)
                {
                    ModelDataSource.CallingDataMethods += handler;
                }

                return(ModelDataSource);
            }

            if (!DesignMode && _currentDataSourceValid && (_currentDataSource != null))
            {
                return(_currentDataSource);
            }

            IDataSource ds           = null;
            string      dataSourceID = DataSourceID;

            if (dataSourceID.Length != 0)
            {
                // Try to find a DataSource control with the ID specified in DataSourceID
                Control control = DataBoundControlHelper.FindControl(this, dataSourceID);
                if (control == null)
                {
                    throw new HttpException(SR.GetString(SR.DataControl_DataSourceDoesntExist, ID, dataSourceID));
                }
                ds = control as IDataSource;
                if (ds == null)
                {
                    throw new HttpException(SR.GetString(SR.DataControl_DataSourceIDMustBeDataControl, ID, dataSourceID));
                }
            }
            return(ds);
        }
コード例 #3
0
        /// <devdoc>
        /// Connects this data bound control to the appropriate DataSourceView
        /// and hooks up the appropriate event listener for the
        /// DataSourceViewChanged event. The return value is the new view (if
        /// any) that was connected to. An exception is thrown if there is
        /// a problem finding the requested view or data source.
        /// </devdoc>
        private DataSourceView ConnectToDataSourceView()
        {
            if (_currentViewValid && !DesignMode)
            {
                // If the current view is correct, there is no need to reconnect
                return(_currentView);
            }

            // Disconnect from old view, if necessary
            if ((_currentView != null) && (_currentViewIsFromDataSourceID))
            {
                // We only care about this event if we are bound through the DataSourceID property
                _currentView.DataSourceViewChanged -= new EventHandler(OnDataSourceViewChanged);
            }

            // Connect to new view
            IDataSource ds = null;

            if (!DesignMode && IsUsingModelBinders)
            {
                if (DataSourceID.Length != 0 || DataSource != null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataControl_ItemType_MultipleDataSources, ID));
                }
                //Let the developer choose a custom ModelDataSource.
                CreatingModelDataSourceEventArgs e = new CreatingModelDataSourceEventArgs();
                OnCreatingModelDataSource(e);
                if (e.ModelDataSource != null)
                {
                    ModelDataSource = e.ModelDataSource;
                }

                //Update the properties of ModelDataSource so that it's ready for data-binding.
                UpdateModelDataSourceProperties(ModelDataSource);

                //Add the CallingDataMethodsEvent
                CallingDataMethodsEventHandler handler = Events[EventCallingDataMethods] as CallingDataMethodsEventHandler;
                if (handler != null)
                {
                    ModelDataSource.CallingDataMethods += handler;
                }

                ds = ModelDataSource;
            }
            else
            {
                string dataSourceID = DataSourceID;

                if (dataSourceID.Length != 0)
                {
                    // Try to find a DataSource control with the ID specified in DataSourceID
                    Control control = DataBoundControlHelper.FindControl(this, dataSourceID);
                    if (control == null)
                    {
                        throw new HttpException(SR.GetString(SR.DataControl_DataSourceDoesntExist, ID, dataSourceID));
                    }
                    ds = control as IDataSource;
                    if (ds == null)
                    {
                        throw new HttpException(SR.GetString(SR.DataControl_DataSourceIDMustBeDataControl, ID, dataSourceID));
                    }
                }
            }

            if (ds == null)
            {
                // DataSource control was not found, construct a temporary data source to wrap the data
                ds = new ReadOnlyDataSource(DataSource, DataMember);
            }
            else
            {
                // Ensure that both DataSourceID as well as DataSource are not set at the same time
                if (DataSource != null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.DataControl_MultipleDataSources, ID));
                }
            }

            // IDataSource was found, extract the appropriate view and return it
            DataSourceView newView = ds.GetView(DataMember);

            if (newView == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.DataControl_ViewNotFound, ID));
            }

            _currentViewIsFromDataSourceID = IsDataBindingAutomatic;
            _currentView = newView;
            if ((_currentView != null) && (_currentViewIsFromDataSourceID))
            {
                // We only care about this event if we are bound through the DataSourceID property
                _currentView.DataSourceViewChanged += new EventHandler(OnDataSourceViewChanged);
            }
            _currentViewValid = true;

            return(_currentView);
        }