Exemplo n.º 1
0
        DataSourceView IDataSource.GetView(string viewName)
        {
            IDataSource source = this._dataSource as IDataSource;

            if (source != null)
            {
                return(source.GetView(viewName));
            }
            return(new ReadOnlyDataSourceView(this, this._dataMember, DataSourceHelper.GetResolvedDataSource(this._dataSource, this._dataMember)));
        }
        /// <devdoc>
        /// Check for IDataSource, IListSource, and IEnumerable, and return an
        /// approprite DataSourceView.
        /// </devdoc>
        DataSourceView IDataSource.GetView(string viewName)
        {
            // Check first for IDataSource
            IDataSource ds = _dataSource as IDataSource;

            if (ds != null)
            {
                return(ds.GetView(viewName));
            }

            IEnumerable enumerable = DataSourceHelper.GetResolvedDataSource(_dataSource, _dataMember);

            return(new ReadOnlyDataSourceView(this, _dataMember, enumerable));
        }
Exemplo n.º 3
0
        /// <include file='doc\ListControl.uex' path='docs/doc[@for="ListControl.OnDataBinding"]/*' />
        /// <internalonly/>
        /// <devdoc>
        /// </devdoc>
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            // create items using the datasource
            IEnumerable dataSource = DataSourceHelper.GetResolvedDataSource(this.DataSource, this.DataMember);

            if (dataSource != null)
            {
                bool fieldsSpecified = false;
                bool formatSpecified = false;

                string textField  = DataTextField;
                string valueField = DataValueField;
                string textFormat = DataTextFormatString;

                Items.Clear();
                ICollection collection = dataSource as ICollection;
                if (collection != null)
                {
                    Items.Capacity = collection.Count;
                }

                if ((textField.Length != 0) || (valueField.Length != 0))
                {
                    fieldsSpecified = true;
                }
                if (textFormat.Length != 0)
                {
                    formatSpecified = true;
                }

                foreach (object dataItem in dataSource)
                {
                    ListItem item = new ListItem();

                    if (fieldsSpecified)
                    {
                        if (textField.Length > 0)
                        {
                            item.Text = DataBinder.GetPropertyValue(dataItem, textField, textFormat);
                        }
                        if (valueField.Length > 0)
                        {
                            item.Value = DataBinder.GetPropertyValue(dataItem, valueField, null);
                        }
                    }
                    else
                    {
                        if (formatSpecified)
                        {
                            item.Text = String.Format(textFormat, dataItem);
                        }
                        else
                        {
                            item.Text = dataItem.ToString();
                        }
                        item.Value = dataItem.ToString();
                    }

                    Items.Add(item);
                }
            }

            // try to apply the cached SelectedIndex and SelectedValue now
            if (cachedSelectedValue != null)
            {
                int cachedSelectedValueIndex = -1;

                cachedSelectedValueIndex = Items.FindByValueInternal(cachedSelectedValue);
                if (-1 == cachedSelectedValueIndex)
                {
                    throw new ArgumentOutOfRangeException("value");
                }

                if ((cachedSelectedIndex != -1) && (cachedSelectedIndex != cachedSelectedValueIndex))
                {
                    throw new ArgumentException(HttpRuntime.FormatResourceString(SR.Attributes_mutually_exclusive, "SelectedIndex", "SelectedValue"));
                }

                SelectedIndex       = cachedSelectedValueIndex;
                cachedSelectedValue = null;
                cachedSelectedIndex = -1;
            }
            else
            {
                if (cachedSelectedIndex != -1)
                {
                    SelectedIndex       = cachedSelectedIndex;
                    cachedSelectedIndex = -1;
                }
            }
        }
Exemplo n.º 4
0
        /// <include file='doc\Repeater.uex' path='docs/doc[@for="Repeater.CreateControlHierarchy"]/*' />
        /// <devdoc>
        ///    A protected method. Creates a control
        ///    hierarchy, with or without the data source as specified.
        /// </devdoc>
        protected virtual void CreateControlHierarchy(bool useDataSource)
        {
            IEnumerable dataSource = null;
            int         count      = -1;

            if (itemsArray != null)
            {
                itemsArray.Clear();
            }
            else
            {
                itemsArray = new ArrayList();
            }

            if (useDataSource == false)
            {
                // ViewState must have a non-null value for ItemCount because we check for
                // this in CreateChildControls
                count = (int)ViewState[ItemCountViewStateKey];
                if (count != -1)
                {
                    dataSource          = new DummyDataSource(count);
                    itemsArray.Capacity = count;
                }
            }
            else
            {
                dataSource = DataSourceHelper.GetResolvedDataSource(this.DataSource, this.DataMember);

                ICollection collection = dataSource as ICollection;
                if (collection != null)
                {
                    itemsArray.Capacity = collection.Count;
                }
            }

            if (dataSource != null)
            {
                ControlCollection controls = Controls;
                RepeaterItem      item;
                ListItemType      itemType;
                int index = 0;

                bool hasSeparators = (separatorTemplate != null);
                count = 0;

                if (headerTemplate != null)
                {
                    CreateItem(-1, ListItemType.Header, useDataSource, null);
                }

                foreach (object dataItem in dataSource)
                {
                    // rather than creating separators after the item, we create the separator
                    // for the previous item in all iterations of this loop.
                    // this is so that we don't create a separator for the last item
                    if (hasSeparators && (count > 0))
                    {
                        CreateItem(index - 1, ListItemType.Separator, useDataSource, null);
                    }

                    itemType = (index % 2 == 0) ? ListItemType.Item : ListItemType.AlternatingItem;
                    item     = CreateItem(index, itemType, useDataSource, dataItem);
                    itemsArray.Add(item);

                    count++;
                    index++;
                }

                if (footerTemplate != null)
                {
                    CreateItem(-1, ListItemType.Footer, useDataSource, null);
                }
            }

            if (useDataSource)
            {
                // save the number of items contained in the repeater for use in round-trips
                ViewState[ItemCountViewStateKey] = ((dataSource != null) ? count : -1);
            }
        }