⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reorderlist.cs

📁 AJAX 应用 实现页面的无刷新
💻 CS
📖 第 1 页 / 共 5 页
字号:
                IDictionary oldValues;
                IOrderedDictionary newValues;
                IDictionary keys;

                // pick up the row values
                //
                PrepareRowValues(e, out oldValues, out newValues, out keys, true);

                DataSourceView view = GetData();

                bool success;

                // get the sort index value for the newly added item.
                //
                int newIndex = GetNewItemSortValue(out success);

                if (success)
                {
                    newValues[SortOrderField] = newIndex;
                }

                if (view != null)
                {
                    // do the actual insert.
                    //
                    view.Insert(newValues,
                        delegate(int rows, Exception ex)
                        {
                            if (ex != null) return false;
                            OnInsertCommand(e);
                            return true;
                        }
                    );
                    return;
                }
            }
            OnInsertCommand(e);
            RequiresDataBinding = true;
        }

        // Called when an "Update" command is fired.
        //        
        private void HandleUpdate(ReorderListCommandEventArgs e, int itemIndex)
        {
            if (IsBoundUsingDataSourceID)
            {
                IDictionary oldValues;
                IOrderedDictionary newValues;
                IDictionary keys;

                if (e == null && itemIndex != -1)
                {
                    e = new ReorderListCommandEventArgs(new CommandEventArgs("Update", null), this, (ReorderListItem)ChildList.Controls[itemIndex]);
                }

                // extract the control values from the row
                //
                PrepareRowValues(e, out oldValues, out newValues, out keys);

                DataSourceView view = GetData();

                if (view != null)
                {
                    // do the update.
                    view.Update(keys, newValues, oldValues,
                        delegate(int rows, Exception ex)
                        {
                            if (ex != null) return false;
                            OnUpdateCommand(e);
                            EditItemIndex = -1;
                            return true;
                        }
                    );
                    return;
                }
            }
            OnUpdateCommand(e);
        }

        /// <summary>
        /// Moves the children of one control to another.
        /// </summary>
        private static void MoveChildren(Control source, Control dest)
        {
            for (int i = source.Controls.Count - 1; i >= 0; i--)
            {
                dest.Controls.AddAt(0, source.Controls[i]);
            }
        }
        
        /// <summary>
        /// Dispatches a bubbled event to the proper handler.        
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1807:AvoidUnnecessaryStringCreation", Justification = "Call to String.op_Equality done by switch statement")]
        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            ReorderListCommandEventArgs ce = args as ReorderListCommandEventArgs;
            if (ce != null)
            {
                OnItemCommand(ce);

                if (ce.CommandArgument != null)
                {

                    string command = ce.CommandName.ToString(CultureInfo.InvariantCulture).ToUpperInvariant();

                    switch (command)
                    {
                        case "INSERT":
                            HandleInsert(ce);
                            return true;
                        case "UPDATE":
                            HandleUpdate(ce, -1);
                            return true;
                        case "EDIT":
                            HandleEdit(ce);
                            return true;
                        case "DELETE":
                            HandleDelete(ce);
                            return true;
                        case "CANCEL":
                            HandleCancel(ce);
                            return true;
                    }
                }
            }
            return false;
        }

        //
        // Event stub functions
        //
        protected virtual void OnItemCreated(EventArgs e)
        {
            Invoke(ItemCreatedKey, e);
        }

        protected virtual void OnItemDataBound(EventArgs e)
        {
            Invoke(ItemDataBoundKey, e);
        }

        protected virtual void OnItemCommand(EventArgs e)
        {
            Invoke(ItemCommandKey, e);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Justification = "Assembly is not localized")]
        protected virtual void OnItemReorder(ReorderListItemReorderEventArgs e)
        {
            try
            {
                if ((DataSource != null || IsBoundUsingDataSourceID ) && !DoReorder(e.OldIndex, e.NewIndex))
                {
                    throw new InvalidOperationException("Can't reorder data source.  It is not a DataSource and does not implement IList.");
                }
            }
            catch (Exception ex)
            {
                CallbackResult = ex.Message;
                throw;
            }
            Invoke(ItemReorderKey, e);
        }       

        protected virtual void OnCancelCommand(EventArgs e)
        {
            Invoke(CancelCommandKey, e);
        }

        protected virtual void OnDeleteCommand(EventArgs e)
        {
            Invoke(DeleteCommandKey, e);
        }

        protected virtual void OnEditCommand(EventArgs e)
        {
            Invoke(EditCommandKey, e);
        }

        protected virtual void OnInsertCommand(EventArgs e)
        {
            Invoke(InsertCommandKey, e);
        }

        protected virtual void OnUpdateCommand(EventArgs e)
        {
            Invoke(UpdateCommandKey, e);
        }

        protected void Invoke(object key, EventArgs e)
        {
            Delegate eventHandler = Events[key];

            if (eventHandler != null)
                eventHandler.DynamicInvoke(this, e);
        }

        /// <summary>
        /// Do the actual databinding
        /// </summary>
        /// <param name="data">our datasource</param>
        protected override void PerformDataBinding(IEnumerable data)
        {
            ClearChildren();
            base.PerformDataBinding(data);
            
            if (IsBoundUsingDataSourceID && EditItemIndex != -1 && EditItemIndex < Controls.Count && IsViewStateEnabled)
            {
                // if we're editing, pick up the bound original values.
                //
                BoundFieldValues.Clear();
                ExtractRowValues(BoundFieldValues, ChildList.Controls[EditItemIndex] as ReorderListItem, false, false);
            }            
        }        

        private void PrepareRowValues(ReorderListCommandEventArgs e, out IDictionary oldValues, out IOrderedDictionary newValues, out IDictionary keys)
        {
            PrepareRowValues(e, out oldValues, out newValues, out keys, false);
        }

        /// <summary>
        /// Extracts the values from an editable row into the given dictionaries.
        /// </summary>
        private void PrepareRowValues(ReorderListCommandEventArgs e, out IDictionary oldValues, out IOrderedDictionary newValues, out IDictionary keys, bool isAddOperation)
        {
            if (!isAddOperation)
            {
                oldValues = CopyDictionary(BoundFieldValues, null);
            }
            else
            {
                oldValues = null;
            }

            newValues = new OrderedDictionary(oldValues == null ? 0 : oldValues.Count);

            if (DataKeyField != null && !isAddOperation)
            {
                keys = new OrderedDictionary(1);

                keys[DataKeyField] = DataKeysArray[e.Item.ItemIndex];
            }
            else
            {
                keys = null;
            }

            // get the values.
            //
            ExtractRowValues(newValues, e.Item, true, isAddOperation);
        }

        /// <summary>
        /// Handle a reorder event from a server postback.
        /// </summary>
        /// <param name="newIndex">The item's new position</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Justification = "Assembly is not localized")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception is caught only for debugging purposes")]
        private void ProcessReorder(int oldIndex, int newIndex)
        {
            try
            {
                System.Diagnostics.Debug.Assert(oldIndex >= 0, "Old index for reorder is < 0 (" + oldIndex + ")");
                System.Diagnostics.Debug.Assert(oldIndex < Items.Count, "Old index for reorder is > items (" + oldIndex + ")");
                System.Diagnostics.Debug.Assert(newIndex >= 0, "New index for reorder is < 0 (" + newIndex + ")");
                System.Diagnostics.Debug.Assert(newIndex < Items.Count, "New index for reorder is > items (" + newIndex + ")");

                // fire the event.
                //
                if ((oldIndex != newIndex) && (Math.Max(oldIndex, newIndex) != DataKeysArray.Count))
                {
                    Control item = Items[oldIndex];
            
                    OnItemReorder(new ReorderListItemReorderEventArgs(item as ReorderListItem, oldIndex, newIndex));
                }
                else
                {
                    //DataBind();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Fail(ex.ToString());
                //TODO WHY ARE SWALLOWING THIS EXCEPTION!!!
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            // show the empty item template if necessary.
            //
            if (ChildList.Controls.Count == 0)
            {
                if (EmptyListTemplate != null)
                {
                    Panel p = new Panel();
                    p.ID = ClientID;
                    EmptyListTemplate.InstantiateIn(p);
                    p.RenderControl(writer);
                }
                return;
            }

            base.RenderContents(writer);
        }
        
        /// <summary>
        /// Performs an update of the specified row with it's current values.
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="causesValidation"></param>
        public void UpdateItem(int rowIndex)
        {
            HandleUpdate(null, rowIndex);
        }

               

        #region IRepeatInfoUser Members
        public Style GetItemStyle(ListItemType itemType, int repeatIndex)
        {
            ReorderListItem item = GetItem(itemType, repeatIndex);
            return item.ControlStyle;
        }

        public bool HasFooter
        {
            get { return false; }
        }

        public bool HasHeader
        {
            get { return false; }
        }

        public bool HasSeparators
        {
            get { return false; }
        }

        public void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
        {
            ReorderListItem item = GetItem(itemType, repeatIndex);
            item.RenderControl(writer);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", Justification = "Assembly is not localized")]
        private ReorderListItem GetItem(ListItemType itemType, int repeatIndex)
        {
            switch (itemType)
            {
                case ListItemType.Item:
                case ListItemType.EditItem:
                    return (ReorderListItem)Controls[repeatIndex];
                case ListItemType.Separator:
          

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -