예제 #1
0
        protected virtual IList<ListViewDataItem> CreateItemsInGroups(ListViewPagedDataSource dataSource, bool dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) {
            // If this is the first time we're creating the control items, we need
            // to locate the groupPlaceholder container.
            // If this is a scenario where we are recreating control items, we already
            // have the cached groupPlaceholder container.
            if (_groupsOriginalIndexOfGroupPlaceholderInContainer == -1) {
                _groupsGroupPlaceholderContainer = GetPreparedContainerInfo(this, false, out _groupsOriginalIndexOfGroupPlaceholderInContainer);
            }

            int groupInsertLocation = _groupsOriginalIndexOfGroupPlaceholderInContainer;
            _groupsItemCreatedCount = 0;

            int itemInsertLocation = 0;
            Control itemPlaceholderContainer = null;

            // Reset the selected index if we have a persisted datakey so we
            // can figure out what index to select based on the key
            ResetPersistedSelectedIndex();

            List<ListViewDataItem> items = new List<ListViewDataItem>();
            int itemIndex = 0;
            int dataItemIndex = 0;

            if (insertPosition == InsertItemPosition.FirstItem) {
                ListViewContainer groupContainer = new ListViewContainer();
                AutoIDControl(groupContainer);
                InstantiateGroupTemplate(groupContainer);
                AddControlToContainer(groupContainer, _groupsGroupPlaceholderContainer, groupInsertLocation);
                groupInsertLocation++;

                itemPlaceholderContainer = GetPreparedContainerInfo(groupContainer, true, out itemInsertLocation);

                ListViewItem insertItem = CreateInsertItem();
                AddControlToContainer(insertItem, itemPlaceholderContainer, itemInsertLocation);
                insertItem.DataBind();
                itemInsertLocation++;
                itemIndex++;
            }

            foreach (object o in dataSource) {
                if (itemIndex % _groupItemCount == 0) {
                    if (itemIndex != 0 && _groupSeparatorTemplate != null) {
                        ListViewContainer groupSeparatorContainer = new ListViewContainer();
                        AutoIDControl(groupSeparatorContainer);
                        InstantiateGroupSeparatorTemplate(groupSeparatorContainer);
                        AddControlToContainer(groupSeparatorContainer, _groupsGroupPlaceholderContainer, groupInsertLocation);
                        groupInsertLocation++;
                    }
                    ListViewContainer groupContainer = new ListViewContainer();
                    AutoIDControl(groupContainer);
                    InstantiateGroupTemplate(groupContainer);
                    AddControlToContainer(groupContainer, _groupsGroupPlaceholderContainer, groupInsertLocation);
                    groupInsertLocation++;

                    itemPlaceholderContainer = GetPreparedContainerInfo(groupContainer, true, out itemInsertLocation);
                }

                ListViewDataItem item = CreateDataItem(dataItemIndex + StartRowIndex, dataItemIndex);

                if (dataBinding) {
                    item.DataItem = o;
                    OrderedDictionary keyTable = new OrderedDictionary(DataKeyNamesInternal.Length);
                    foreach (string keyName in DataKeyNamesInternal) {
                        object keyValue = DataBinder.GetPropertyValue(o, keyName);
                        keyTable.Add(keyName, keyValue);
                    }
                    if (keyArray.Count == dataItemIndex) {
                        keyArray.Add(new DataKey(keyTable, DataKeyNamesInternal));
                    }
                    else {
                        keyArray[dataItemIndex] = new DataKey(keyTable, DataKeyNamesInternal);
                    }
                }
                // If persisted selection is enabled and we have a data key then compare it to get the selected index
                if (EnablePersistedSelection) {
                    if (dataItemIndex < keyArray.Count) {
                        DataKey currentKey = (DataKey)keyArray[dataItemIndex];
                        SetPersistedDataKey(dataItemIndex, currentKey);
                    }
                }

                InstantiateItemTemplate(item, dataItemIndex);

                OnItemCreated(new ListViewItemEventArgs(item));

                if (itemIndex % _groupItemCount != 0 && _itemSeparatorTemplate != null) {
                    ListViewContainer itemSeparatorContainer = new ListViewContainer();
                    InstantiateItemSeparatorTemplate(itemSeparatorContainer);
                    AddControlToContainer(itemSeparatorContainer, itemPlaceholderContainer, itemInsertLocation);
                    itemInsertLocation++;
                }


                AddControlToContainer(item, itemPlaceholderContainer, itemInsertLocation);
                itemInsertLocation++;
                items.Add(item);

                if (dataBinding) {
                    item.DataBind();
                    OnItemDataBound(new ListViewItemEventArgs(item));
                    item.DataItem = null;
                }

                itemIndex++;
                dataItemIndex++;
            }

            if (insertPosition == InsertItemPosition.LastItem) {
                if (itemIndex % _groupItemCount == 0) {
                    // start a new group
                    if (itemIndex != 0 && _groupSeparatorTemplate != null) {
                        ListViewContainer groupSeparatorContainer = new ListViewContainer();
                        AutoIDControl(groupSeparatorContainer);
                        InstantiateGroupSeparatorTemplate(groupSeparatorContainer);
                        AddControlToContainer(groupSeparatorContainer, _groupsGroupPlaceholderContainer, groupInsertLocation);
                        groupInsertLocation++;
                    }
                    ListViewContainer groupContainer = new ListViewContainer();
                    AutoIDControl(groupContainer);
                    InstantiateGroupTemplate(groupContainer);
                    AddControlToContainer(groupContainer, _groupsGroupPlaceholderContainer, groupInsertLocation);
                    groupInsertLocation++;

                    itemPlaceholderContainer = GetPreparedContainerInfo(groupContainer, true, out itemInsertLocation);
                }

                // use the existing group
                if (itemIndex % _groupItemCount != 0 && _itemSeparatorTemplate != null) {
                    ListViewContainer itemSeparatorContainer = new ListViewContainer();
                    InstantiateItemSeparatorTemplate(itemSeparatorContainer);
                    AddControlToContainer(itemSeparatorContainer, itemPlaceholderContainer, itemInsertLocation);
                    itemInsertLocation++;
                }

                ListViewItem insertItem = CreateInsertItem();
                AddControlToContainer(insertItem, itemPlaceholderContainer, itemInsertLocation);
                insertItem.DataBind();
                itemInsertLocation++;
                itemIndex++;
            }

            // fill in the rest of the items if there's an emptyItemTemplate
            if (_emptyItemTemplate != null) {
                while (itemIndex % _groupItemCount != 0) {
                    if (_itemSeparatorTemplate != null) {
                        ListViewContainer itemSeparatorContainer = new ListViewContainer();
                        InstantiateItemSeparatorTemplate(itemSeparatorContainer);
                        AddControlToContainer(itemSeparatorContainer, itemPlaceholderContainer, itemInsertLocation);
                        itemInsertLocation++;
                    }

                    ListViewItem emptyItem = CreateEmptyItem();
                    AddControlToContainer(emptyItem, itemPlaceholderContainer, itemInsertLocation);
                    itemInsertLocation++;
                    itemIndex++;
                }
            }

            _groupsItemCreatedCount = groupInsertLocation - _groupsOriginalIndexOfGroupPlaceholderInContainer;

            return items;
        }
예제 #2
0
        protected virtual IList<ListViewDataItem> CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, bool dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) {
            // If this is the first time we're creating the control items, we need
            // to locate the itemPlaceholder container.
            // If this is a scenario where we are recreating control items, we already
            // have the cached itemPlaceholder container.
            if (_noGroupsOriginalIndexOfItemPlaceholderInContainer == -1) {
                _noGroupsItemPlaceholderContainer = GetPreparedContainerInfo(this, true, out _noGroupsOriginalIndexOfItemPlaceholderInContainer);
            }

            // We need to keep track of where we're inserting items and how many items we have
            // inserted so that if we need to remove them we know what to do.
            int itemInsertLocation = _noGroupsOriginalIndexOfItemPlaceholderInContainer;

            List<ListViewDataItem> items = new List<ListViewDataItem>();
            int itemIndex = 0;
            int dataItemIndex = 0;

            if (insertPosition == InsertItemPosition.FirstItem) {
                ListViewItem insertItem = CreateInsertItem();
                AddControlToContainer(insertItem, _noGroupsItemPlaceholderContainer, itemInsertLocation);
                insertItem.DataBind();
                itemInsertLocation++;
                itemIndex++;
            }
            // Reset the selected index if we have a persisted datakey so we
            // can figure out what index to select based on the key
            ResetPersistedSelectedIndex();

            foreach (object o in dataSource) {
                if (itemIndex != 0 && _itemSeparatorTemplate != null) {
                    ListViewContainer itemSeparatorContainer = new ListViewContainer();
                    AutoIDControl(itemSeparatorContainer);
                    InstantiateItemSeparatorTemplate(itemSeparatorContainer);
                    AddControlToContainer(itemSeparatorContainer, _noGroupsItemPlaceholderContainer, itemInsertLocation);
                    itemInsertLocation++;
                }

                ListViewDataItem item = CreateDataItem(dataItemIndex + dataSource.StartRowIndex, dataItemIndex);
                AutoIDControl(item);

                if (dataBinding) {
                    item.DataItem = o;
                    OrderedDictionary keyTable = new OrderedDictionary(DataKeyNamesInternal.Length);
                    foreach (string keyName in DataKeyNamesInternal) {
                        object keyValue = DataBinder.GetPropertyValue(o, keyName);
                        keyTable.Add(keyName, keyValue);
                    }
                    if (keyArray.Count == dataItemIndex) {
                        keyArray.Add(new DataKey(keyTable, DataKeyNamesInternal));
                    }
                    else {
                        keyArray[dataItemIndex] = new DataKey(keyTable, DataKeyNamesInternal);
                    }
                }

                // If persisted selection is enabled and we have a data key then compare it to get the selected index
                if (EnablePersistedSelection) {
                    if (dataItemIndex < keyArray.Count) {
                        DataKey currentKey = (DataKey)keyArray[dataItemIndex];
                        SetPersistedDataKey(dataItemIndex, currentKey);
                    }
                }
                
                InstantiateItemTemplate(item, dataItemIndex);
                

                OnItemCreated(new ListViewItemEventArgs(item));
                AddControlToContainer(item, _noGroupsItemPlaceholderContainer, itemInsertLocation);
                itemInsertLocation++;
                items.Add(item);

                if (dataBinding) {
                    item.DataBind();
                    OnItemDataBound(new ListViewItemEventArgs(item));
                    item.DataItem = null;
                }

                dataItemIndex++;
                itemIndex++;
            }

            if (insertPosition == InsertItemPosition.LastItem) {
                if (_itemSeparatorTemplate != null) {
                    ListViewContainer itemSeparatorContainer = new ListViewContainer();
                    AutoIDControl(itemSeparatorContainer);
                    InstantiateItemSeparatorTemplate(itemSeparatorContainer);
                    AddControlToContainer(itemSeparatorContainer, _noGroupsItemPlaceholderContainer, itemInsertLocation);
                    itemInsertLocation++;
                }

                ListViewItem insertItem = CreateInsertItem();
                AddControlToContainer(insertItem, _noGroupsItemPlaceholderContainer, itemInsertLocation);
                insertItem.DataBind();
                itemInsertLocation++;
                itemIndex++;
            }

            _noGroupsItemCreatedCount = itemInsertLocation - _noGroupsOriginalIndexOfItemPlaceholderInContainer;

            return items;
        }