private DataSourceView ConnectToDataSourceView()
 {
     if (!this._currentViewValid || base.DesignMode)
     {
         if ((this._currentView != null) && this._currentViewIsFromDataSourceID)
         {
             this._currentView.DataSourceViewChanged -= new EventHandler(this.OnDataSourceViewChanged);
         }
         IDataSource source       = null;
         string      dataSourceID = this.DataSourceID;
         if (dataSourceID.Length != 0)
         {
             Control control = DataBoundControlHelper.FindControl(this, dataSourceID);
             if (control == null)
             {
                 throw new HttpException(System.Web.SR.GetString("DataControl_DataSourceDoesntExist", new object[] { this.ID, dataSourceID }));
             }
             source = control as IDataSource;
             if (source == null)
             {
                 throw new HttpException(System.Web.SR.GetString("DataControl_DataSourceIDMustBeDataControl", new object[] { this.ID, dataSourceID }));
             }
         }
         if (source == null)
         {
             source = new ReadOnlyDataSource(this.DataSource, this.DataMember);
         }
         else if (this.DataSource != null)
         {
             throw new InvalidOperationException(System.Web.SR.GetString("DataControl_MultipleDataSources", new object[] { this.ID }));
         }
         DataSourceView view = source.GetView(this.DataMember);
         if (view == null)
         {
             throw new InvalidOperationException(System.Web.SR.GetString("DataControl_ViewNotFound", new object[] { this.ID }));
         }
         this._currentViewIsFromDataSourceID = this.IsBoundUsingDataSourceID;
         this._currentView = view;
         if ((this._currentView != null) && this._currentViewIsFromDataSourceID)
         {
             this._currentView.DataSourceViewChanged += new EventHandler(this.OnDataSourceViewChanged);
         }
         this._currentViewValid = true;
     }
     return(this._currentView);
 }
        /// <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;
            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 = IsBoundUsingDataSourceID;
            _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);
        }