AjaxControlToolkit.Accordion.ConnectToDataSourceView C# (CSharp) Method

ConnectToDataSourceView() private method

private ConnectToDataSourceView ( ) : System.Web.UI.DataSourceView
return System.Web.UI.DataSourceView
        DataSourceView ConnectToDataSourceView()
        {
            // If the current view is correct, there is no need to reconnect
            if(_currentViewValid && !DesignMode)
                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;
            var dataSourceID = DataSourceID;

            if(!String.IsNullOrEmpty(dataSourceID)) {
                // Try to find a DataSource control with the ID specified in DataSourceID
                var control = NamingContainer.FindControl(dataSourceID);
                if(control == null)
                    throw new HttpException(String.Format(CultureInfo.CurrentCulture, "DataSource '{1}' for control '{0}' doesn't exist", ID, dataSourceID));
                ds = control as IDataSource;
                if(ds == null)
                    throw new HttpException(String.Format(CultureInfo.CurrentCulture, "'{1}' is not a data source for control '{0}'.", ID, dataSourceID));
            }

            if(ds == null)
                // DataSource control was not found, construct a temporary data source to wrap the data
                return null;
            else
                // Ensure that both DataSourceID as well as DataSource are not set at the same time
                if(DataSource != null)
                    throw new InvalidOperationException("DataSourceID and DataSource can't be set at the same time.");

            // IDataSource was found, extract the appropriate view and return it
            var newView = ds.GetView(DataMember);
            if(newView == null)
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "DataSourceView not found for control '{0}'", ID));

            _currentViewIsFromDataSourceID = IsBoundUsingDataSourceID;
            _currentView = newView;
            // If we're bound through the DataSourceID proeprty, then we care about this event
            if((_currentView != null) && (_currentViewIsFromDataSourceID))
                _currentView.DataSourceViewChanged += new EventHandler(OnDataSourceViewChanged);
            _currentViewValid = true;

            return _currentView;
        }