private void InsertItem(int itemIndex, object dataItem, bool selectItem)
        {
            // check if the current page is full
            IOSList rl = DataSource;

            // update the datasource
            rl.Insert(dataItem, itemIndex);

            // increase higher item indexes in the iterator
            foreach (IIteratorItem otherItem in Controls)
            {
                if (otherItem.ItemIndex >= itemIndex)
                {
                    otherItem.ItemIndex++;
                }
            }

            // create an item at the end of the list (and don't databind...)
            CreateItem(dataItem, itemIndex, false);

            // update item count
            ViewStateAttributes.SetInViewState("NumItems", ItemCount, null);

            // update item ids and indexes in the viewstate
            StoreViewStateItemIdsandIndexes();

            var previousSelectedIndex   = SelectedIndex;
            var previousDataSourceIndex = rl.CurrentRowNumber;

            SelectedIndex = itemIndex;

            // set the record list position
            rl.SetPosition(itemIndex);

            // databind the created item
            DataBindLastCreatedItem();

            if (!selectItem)
            {
                // revert the record list position
                rl.SetPosition(previousDataSourceIndex);
                SelectedIndex = previousSelectedIndex;
            }
        }
        protected override bool OnBubbleEvent(object source, EventArgs e)
        {
            // set the selected index
            if (e is DataGridCommandEventArgs)
            {
                SelectedIndex = ((DataGridCommandEventArgs)e).Item.ItemIndex;
            }
            else if (e is AjaxEventArgs)
            {
                // ajax bubble up event
                // find the datagriditem that is parent of the sender control to set the selected index
                Control ctrl = source as Control;
                while (!(ctrl is DataGridItem))
                {
                    ctrl = ctrl.Parent;
                }
                SelectedIndex = ((DataGridItem)ctrl).ItemIndex;

                // create a datagrid command ajax event args instance, so that it can be trapped onbubble up by the table records select handler
                e = new DataGridCommandAjaxEventArgs((DataGridItem)ctrl, (Control)source, (AjaxEventArgs)e);
            }
            // set the datasource position so that the current record is set if the list is not empty and in memory
            IOSList rl = DataSource;

            if (rl != null && rl.Length > 0 && SelectedIndex != -1)
            {
                try {
                    rl.SetPosition(SelectedIndex);
                } catch (ArgumentOutOfRangeException) {
                    throw new HEMessageException(MR.GetMessage(MessageId.AjaxListModified, "Table Records", this.ID));
                }
            }

            // keep the current row number of the record list before bubbling up the event
            int beforeBubbleCurrentRowNumber = (rl != null) ? rl.CurrentRowNumber : 0;

            // follow up the event
            bool ret = base.OnBubbleEvent(source, e);

            // store viewstate changes for the current row. Skip if:
            // - the record list is empty
            // - the current row number is not the same as the one before bubbling up event - happens when the list is iterated inside the action
            if (ret && rl != null && (rl.Length > 0) && (SelectedIndex != -1) && (beforeBubbleCurrentRowNumber == rl.CurrentRowNumber))
            {
                SetViewStateStorage(rl.Current, SelectedIndex);
            }

            return(ret);
        }
        public void RefreshItem(int dataItemIndex)
        {
            // set the datasource position
            IOSList rl = DataSource;

            rl.SetPosition(dataItemIndex);
            SelectedIndex = dataItemIndex;

            // databind the correspondent datagriditem
            int             controlIndex = GetItemControlIndex(dataItemIndex);
            OSDataGridTable child        = (OSDataGridTable)Controls[0];
            OSDataGridItem  item         = (OSDataGridItem)child.Rows[controlIndex];

            // initialize item
            item.DataItem = rl.Current;
            InitializeItem(item, false);
            item.DataBind();
        }
        public void RefreshItem(int dataItemIndex)
        {
            // set the datasource position
            IOSList rl = DataSource;

            rl.SetPosition(dataItemIndex);
            SelectedIndex = dataItemIndex;

            // databind the correspondent item
            int          controlIndex = GetItemControlIndex(dataItemIndex);
            RepeaterItem item         = (RepeaterItem)Controls[controlIndex];

            item.DataItem = rl.Current;
            item.DataBind();

            InvisibleItem invItem = (InvisibleItem)Controls[controlIndex + 1];

            invItem.DataItem = rl.Current;
            invItem.DataBind();
        }
        protected override bool OnBubbleEvent(object sender, EventArgs args)
        {
            if (_itemCommandName == null)
            {
                return(true);
            }

            if (((sender.GetType() == typeof(Button) || sender.GetType() == typeof(LinkButton)) && (args is CommandEventArgs)) ||
                args is AjaxEventArgs)
            {
                // Get the method
                Control    ParentPage = Utils.GetOwnerOfControl(this);
                MethodInfo info       = ParentPage.GetType().GetMethod(_itemCommandName);
                if (info == null)
                {
                    throw (new Exception("Could not find method " + _itemCommandName));
                }
                // Get the item that was clicked
                Control ctrl = (Control)sender;
                while (ctrl.GetType() != typeof(RepeaterItem))
                {
                    ctrl = ctrl.Parent;
                }
                // Set selected item index
                SelectedIndex = ((RepeaterItem)ctrl).ItemIndex;
                // set the datasource position so that the current record is set if the list is not empty and in memory
                IOSList rl = DataSource;
                if (rl != null && rl.Length > 0)
                {
                    try {
                        rl.SetPosition(SelectedIndex);
                    } catch (ArgumentOutOfRangeException) {
                        throw new HEMessageException(MR.GetMessage(MessageId.AjaxListModified, "List Records", this.ID));
                    }
                }

                // keep the current row number of the record list before bubbling up the event
                int beforeBubbleCurrentRowNumber = (rl != null) ? rl.CurrentRowNumber : 0;

                try {
                    IteratorCommandEventArgs iteratorArgs;
                    if (args is AjaxEventArgs)
                    {
                        // ajax event, must create IteratorCommandAjaxEventArgs to be trapped by the select handler of the iterator
                        iteratorArgs = new IteratorCommandAjaxEventArgs(SelectedIndex, (Control)sender, (AjaxEventArgs)args);
                    }
                    else
                    {
                        // submit event, use IteratorCommandEventArgs
                        iteratorArgs = new IteratorCommandEventArgs(SelectedIndex, (Control)sender);
                    }
                    // invoke the select method
                    info.Invoke(ParentPage, new object[] { this, iteratorArgs });
                } catch (Exception exception) {
                    var exceptionAux = exception;
                    while (exceptionAux != null && exception.GetType() == typeof(System.Reflection.TargetInvocationException))
                    {
                        exceptionAux = exceptionAux.InnerException;
                    }
                    if (exceptionAux.GetType() == typeof(System.Threading.ThreadAbortException))
                    {
                        return(true);
                    }
                    throw;
                }

                // store viewstate changes for the current row. Skip if:
                // - the record list is empty
                // - the current row number is not the same as the one before bubbling up event - happens when the list is iterated inside the action
                if (rl != null && rl.Length > 0 && SelectedIndex != -1 && beforeBubbleCurrentRowNumber == rl.CurrentRowNumber)
                {
                    SetViewStateStorage(rl.Current, SelectedIndex);
                }

                return(true);
            }
            return(true);
        }
        /// <summary>
        /// Inserts an item to the datagrid
        /// </summary>
        /// <param name="param name="dataItemIndex"></param>
        /// <param name="dataItem"></param>
        public void InsertItem(int dataItemIndex, object dataItem)
        {
            // check if the current page is full
            IOSList rl = DataSource;

            // update the datasource
            rl.Insert(dataItem, dataItemIndex);

            // set the record list position and databind the item
            rl.SetPosition(dataItemIndex);

            // create an item at the end of the list
            OSDataGridItem        item = (OSDataGridItem)CreateItem(dataItemIndex, dataItemIndex, ListItemType.Item);
            DataGridItemEventArgs e    = new DataGridItemEventArgs(item);

            // initialize item
            item.DataItem = dataItem;
            InitializeItem(item, true);
            this.OnItemCreated(e);

            OSDataGridTable child = (OSDataGridTable)Controls[0];

            // remove any empty message row...
            if (((OSDataGridItem)child.Rows[child.Rows.Count - 1]).IsEmptyMessageItem)
            {
                child.Rows.RemoveAt(child.Rows.Count - 1);
            }

            // insert the item before the footer and pager items
            int footerIndex = child.Rows.Count - 2;

            int footerId;

            if (child.Rows[footerIndex].ID != null)
            {
                footerId = OSDataGridItem.GetIntegerId(child.Rows[footerIndex].ID, EnableLegacyRendering);
            }
            else
            {
                footerId = footerIndex + 1;
            }

            child.Rows.AddAt(footerIndex, item);

            // generate a new item id for this control
            int id = footerId;

            item.ID = OSDataGridItem.FormatId(id, EnableLegacyRendering);

            // increase the footer and pager items, so that no repateated ids exist in the page
            child.Rows[child.Rows.Count - 2].ID = OSDataGridItem.FormatId(id + 1, EnableLegacyRendering);
            child.Rows[child.Rows.Count - 1].ID = OSDataGridItem.FormatId(id + 2, EnableLegacyRendering);

            ViewState["_!ItemCount"] = rl.Length;
            _isEmpty = false;

            // increase higher item indexes in the data grid
            foreach (OSDataGridItem otherItem in child.Rows)
            {
                if (otherItem.ItemIndex >= dataItemIndex && otherItem != item)
                {
                    otherItem.SetItemIndex(otherItem.ItemIndex + 1);
                }
            }

            // update row ids in the viewstate
            StoreViewStateRowIdsandItemIndexes();

            SelectedIndex = dataItemIndex;

            item.DataBind();
            this.OnItemDataBound(e);

            item.DataItem = null;
        }