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

📄 reorderlist.cs

📁 AJAX 应用 实现页面的无刷新
💻 CS
📖 第 1 页 / 共 5 页
字号:
        [TemplateContainer(typeof(IDataItemContainer), BindingDirection.TwoWay)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DefaultValue("")]
        public ITemplate ItemTemplate
        {
            get { return _itemTemplate; }
            set { _itemTemplate = value; }
        }

        [Browsable(false)]
        public ReorderListItemCollection Items
        {
            get            
            {
                EnsureDataBound();
                return new ReorderListItemCollection(this);
            }
        }

        /// <summary>
        /// The type of layout to apply to the items.  If "Table" is selected, the 
        /// DragHandleAlignment property is used to lay out the items 
        /// in relation to the drag handle.  If not, the items are simply wrapped in Panel
        /// controls and can be positioned using CSS.
        /// </summary>
        [DefaultValue(ReorderListItemLayoutType.Table)]
        public ReorderListItemLayoutType LayoutType
        {
            get
            {
                return _layoutType;
            }
            set
            {
                _layoutType = value;
            }
        }

        [DefaultValue("true")]
        public bool PostBackOnReorder
        {
            get
            {
                return GetPropertyValue("PostBackOnReorder", false);

            }
            set
            {
                SetPropertyValue("PostBackOnReorder", value);
                
            }
        }

        /// <summary>
        /// The name of the column which controls the sort order of the rows in the data base.
        /// </summary>
        [DefaultValue("")]
        public string SortOrderField
        {
            get
            {
                return GetPropertyValue("SortOrderField", "");
            }
            set
            {
                SetPropertyValue("SortOrderField", value);
            }
        }

        /// <summary>
        /// The template to use as the drop visual when the user is dragging an item around.  
        /// This template is not data bindable.
        /// </summary>
        [Browsable(false)]
        [TemplateContainer(typeof(ReorderListItem))]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        [DefaultValue("")]
        public ITemplate ReorderTemplate
        {
            get { return _reorderTemplate; }
            set { _reorderTemplate = value; }
        }

        /// <summary>
        /// The resource URL to use for loading the script associated with this control.
        /// </summary>
        private string ScriptResourceUrl
        {
            get
            {
                string url = Page.Request.Url.AbsoluteUri;
                string hostPath = url.Substring(0, url.Length - Page.Request.Url.PathAndQuery.Length);

                url = hostPath + Page.ClientScript.GetWebResourceUrl(typeof(ReorderList), "WebUtilControls.DragDropList.js").Replace("&", "&amp;");
                return url;
            }
        }

        /// <summary>
        /// Determines whether the InsertItem is shown.  If this value is not set and
        /// an InsertItemTemplate is set, this defaults to true.
        /// </summary>
        [DefaultValue(false)]
        public bool ShowInsertItem
        {
            get
            {
                return GetPropertyValue("ShowInsertItem", InsertItemTemplate != null);
            }
            set
            {
                SetPropertyValue("ShowInsertItem", value);
            }
        }

        /// <summary>
        /// This control renders a DIV
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        public ReorderList()
        {
            
        }

        /// <summary>
        /// Helper method to copy the values from one dictionary to another.
        /// </summary>
        private static IDictionary CopyDictionary(IDictionary source, IDictionary dest)
        {
            if (dest == null)
            {
                dest = new OrderedDictionary(source.Count);
            }

            foreach (DictionaryEntry de in source)
            {
                dest[de.Key] = de.Value;
            }

            return dest;
        }

        private void ClearChildren()
        {
            ChildList.Controls.Clear();            
            _dropTemplateControl = null;

            if (_draggableItems != null) {
                foreach (DraggableListItemInfo item in _draggableItems) {
                    if (item.Extender != null) {                       
                        item.Extender.Dispose();
                    }
                }
            }
            _draggableItems = null;

            for (int i = Controls.Count - 1; i >= 0; i--) {
                if (Controls[i] is DropWatcherExtender) {
                    Controls[i].Dispose();
                    
                }
            }
        }

        /// <summary>
        /// This method does the heavy lifting of building the control hierarchy from a dataSource.
        /// 
        /// If no datasource is passed in, for example when a postback occurs, it creates a dummy list
        /// based on the number of items it last had.
        /// </summary>
        /// <param name="dataSource"></param>
        /// 
        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {
            ClearChildren();

            int countDelta = 0;
            
            ArrayList keysArray = DataKeysArray;
            itemsArray = new ArrayList();
            int count = DesignMode ? 1 : 0;
                       
            if (dataBinding)
            {
                keysArray.Clear();

                ICollection c = dataSource as ICollection;

                if (c != null)
                {
                    keysArray.Capacity = c.Count;
                    itemsArray.Capacity = c.Count;
                }
            }

            if (dataSource != null)
            {
                string keyField = DataKeyField;
                bool storeKey = (dataBinding && !string.IsNullOrEmpty(keyField));
                
                bool hasDragHandle = AllowReorder && (DragHandleTemplate != null);

                count = 0;
                int index = 0;

                // for each item in the list, create it's ReorderListItem
                // which gets automatically added to the parent.
                //
                foreach (object dataItem in dataSource)
                {
                    if (storeKey)
                    {
                        keysArray.Add(DataBinder.GetPropertyValue(dataItem, keyField));
                    }

                    ListItemType itemType = ListItemType.Item;

                    if (index == EditItemIndex)
                    {
                        itemType = ListItemType.EditItem;
                    }

                    CreateItem(index, dataBinding, dataItem, itemType, hasDragHandle);

                    count++;
                    index++;
                }                               
                               
                // add the insert item if needed.
                //
                if (ShowInsertItem && InsertItemTemplate != null)
                {
                    CreateInsertItem(index);
                    countDelta++;
                }
            }

            if (AllowReorder && count > 1 && _draggableItems != null)
            {
                // we should now have a list of items that can be dragged,
                // setup the the extender behaviors for them.
                //
                foreach (DraggableListItemInfo dlii in _draggableItems)
                {
                    dlii.Extender = new DraggableListItemExtender();
                    dlii.Extender.TargetControlID = dlii.TargetControl.ID;
                    dlii.Extender.Handle = dlii.HandleControl.ClientID;
                    dlii.Extender.ID = String.Format(CultureInfo.InvariantCulture, "{0}_{1}", this.ID, dlii.Extender.TargetControlID);
                    this.Controls.Add(dlii.Extender);
                }
                // render our drag templates.
                //
                Control dropArea, emptyItem;
                GetDropTemplateControl(out dropArea, out emptyItem);
                
                _dropWatcherExtender = new DropWatcherExtender();
                _dropWatcherExtender.ArgReplaceString = ArgReplace;                
                _dropWatcherExtender.CallbackCssStyle = CallbackCssStyle;
                _dropWatcherExtender.DropLayoutElement = dropArea.ID;
                if (PostBackOnReorder)
                {
                    _dropWatcherExtender.PostBackCode = Page.ClientScript.GetPostBackEventReference(this, ArgReplace);
                }
                else
                {
                    _dropWatcherExtender.PostBackCode = Page.ClientScript.GetCallbackEventReference(this, "'" + ArgReplace + "'", ArgSuccess, "'" + ArgContext + "'", ArgError, true);
                    _dropWatcherExtender.ArgContextString = ArgContext;
                    _dropWatcherExtender.ArgSuccessString = ArgSuccess;
                    _dropWatcherExtender.ArgErrorString = ArgError;
                }
                _dropWatcherExtender.EnableClientState = !PostBackOnReorder;

                _dropWatcherExtender.BehaviorID = this.UniqueID + "_dItemEx";
                _dropWatcherExtender.TargetControlID = ChildList.ID;
                
                this.Controls.Add(_dropWatcherExtender);                
            }

            return ChildList.Controls.Count - countDelta;
        }


        /// <summary>
        /// Creates the control that will be our reorder template.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="reorderKey"></param>
        /// <returns></returns>
        private Control CreateReorderArea(int index, string reorderKey)
        {
            Panel reorderContainer = new Panel();
            reorderContainer.ID = String.Format(CultureInfo.InvariantCulture, "__drop{1}{0}", index, reorderKey);

            if (ReorderTemplate != null)
            {
                ReorderTemplate.InstantiateIn(reorderContainer);
            }            
            return reorderContainer;
        }
        
        /// <summary>
        /// Creates the InsertItem object for a given index.
        /// </summary>
        protected virtual ReorderListItem CreateInsertItem(int index)
        {
            if (InsertItemTemplate != null && ShowInsertItem)
            {
                ReorderListItem item = new ReorderListItem(index, true);
                InsertItemTemplate.InstantiateIn(item);
                ChildList.Controls.Add(item);
                return item;
            }
            return null;
        }

        /// <summary>
        /// Builds the drag handle element and the table which controls it's alignment
        /// </summary>
        /// <param name="item">The item that's currently at this index</param>        
        protected virtual void CreateDragHandle(ReorderListItem item)
        {
            if (!AllowReorder) return;

            Control dragHolder = item;
            if (DragHandleTemplate != null) {

                Control outerItem = null;
                Control itemParent = null;

                if (LayoutType == ReorderListItemLayoutType.User)
                {

                    outerItem = new Panel();
                    Panel itemCell = new Panel();
                    Panel handleCell = new Panel();

                    dragHolder = handleCell;
                    itemParent = itemCell;

                    if (DragHandleAlignment == ReorderHandleAlignment.Left ||
                        DragHandleAlignment == ReorderHandleAlignment.Top)
                    {
                        outerItem.Controls.Add(handleCell);
                        outerItem.Controls.Add(itemCell);
                    }
                    else
                    {
                        outerItem.Controls.Add(itemCell);
                        outerItem.Controls.Add(handleCell);
                    }
                }

⌨️ 快捷键说明

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