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

📄 reorderlist.cs

📁 AJAX 应用 实现页面的无刷新
💻 CS
📖 第 1 页 / 共 5 页
字号:
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Threading;
using System.Web.Script;

using AjaxControlToolkit.Design;

using BindingDirection = System.ComponentModel.BindingDirection;
using System.Data;

namespace AjaxControlToolkit
{
    /// <summary>
    /// This class implements a reorderable, data bindable list control.
    /// 
    /// It demonstrates writing a control that is AJAX-aware in that it creates and
    /// manages a set of ASP.NET AJAX extenders. to imporove it's client experience.
    /// 
    /// It is also data aware.  When supplied with a database that matches an expected format,
    /// it will automatically update the sort order of the items.  The database must have an integer-typed
    /// column that this list can have "ownership" of.  By setting this column name into the "SortOrderField" property,
    /// reorders will happen automatically.
    /// 
    /// </summary>
    [Designer("AjaxControlToolkit.ReorderListDesigner, AjaxControlToolkit")]
    [System.Drawing.ToolboxBitmap(typeof(ReorderList), "ReorderList.ReorderList.ico")]
    public class ReorderList : CompositeDataBoundControl, IRepeatInfoUser, INamingContainer, ICallbackEventHandler, IPostBackEventHandler
    {
        // key fields for various things.
        private static object ItemCommandKey = new object();
        private static object CancelCommandKey = new object();
        private static object EditCommandKey = new object();
        private static object DeleteCommandKey = new object();
        private static object UpdateCommandKey = new object();
        private static object InsertCommandKey = new object();
        private static object ItemDataBoundKey = new object();
        private static object ItemCreatedKey = new object();
        private static object ItemReorderKey = new object();
        private static object KeysKey = new object();
        
        /// <summary>
        ///  All of our event handlers.
        /// </summary>
        public event EventHandler<ReorderListCommandEventArgs> ItemCommand { add { Events.AddHandler(ItemCommandKey, value); } remove { Events.RemoveHandler(ItemCommandKey, value); } }
        public event EventHandler<ReorderListCommandEventArgs> CancelCommand { add { Events.AddHandler(CancelCommandKey, value); } remove { Events.RemoveHandler(CancelCommandKey, value); } }
        public event EventHandler<ReorderListCommandEventArgs> DeleteCommand { add { Events.AddHandler(DeleteCommandKey, value); } remove { Events.RemoveHandler(DeleteCommandKey, value); } }
        public event EventHandler<ReorderListCommandEventArgs> EditCommand { add { Events.AddHandler(EditCommandKey, value); } remove { Events.RemoveHandler(EditCommandKey, value); } }
        public event EventHandler<ReorderListCommandEventArgs> InsertCommand { add { Events.AddHandler(InsertCommandKey, value); } remove { Events.RemoveHandler(InsertCommandKey, value); } }
        public event EventHandler<ReorderListCommandEventArgs> UpdateCommand { add { Events.AddHandler(UpdateCommandKey, value); } remove { Events.RemoveHandler(UpdateCommandKey, value); } }
        public event EventHandler<ReorderListItemEventArgs> ItemDataBound { add { Events.AddHandler(ItemDataBoundKey, value); } remove { Events.RemoveHandler(ItemDataBoundKey, value); } }
        public event EventHandler<ReorderListItemEventArgs> ItemCreated { add { Events.AddHandler(ItemCreatedKey, value); } remove { Events.RemoveHandler(ItemCreatedKey, value); } }
        public event EventHandler<ReorderListItemReorderEventArgs> ItemReorder { add { Events.AddHandler(ItemReorderKey, value); } remove { Events.RemoveHandler(ItemReorderKey, value); } }

        /// <summary>
        /// The actual list control.  This control actually renders a DIV with some children:
        /// 
        /// The UL control
        /// The DropWatcherExtender
        /// The DraggableListitemExtender
        /// The drop template control
        /// </summary>
        private BulletedList _childList;
        
        /// <summary>
        /// A control that we generate for the drop template
        /// </summary>
        private Control _dropTemplateControl;

        /// <summary>
        /// Our template member variables.
        /// </summary>
        private ITemplate _reorderTemplate;
        private ITemplate _itemTemplate;
        private ITemplate _editItemTemplate;
        private ITemplate _insertItemTemplate;
        private ITemplate _dragHandleTemplate;
        private ITemplate _emptyListTemplate;

        /// <summary>
        /// The list of items that can be dragged around.  We maintain this list so we know
        /// what to generate later in the draggableListItems Extender
        /// </summary>
        private List<DraggableListItemInfo> _draggableItems;
        private DropWatcherExtender _dropWatcherExtender;

        private class DraggableListItemInfo
        {
            public Control TargetControl;
            public Control HandleControl;
            public DraggableListItemExtender Extender;
        }

        private ArrayList itemsArray;
        private const string ArgReplace = "_~Arg~_";
        private const string ArgContext = "_~Context~_";
        private const string ArgSuccess = "_~Success~_";
        private const string ArgError   = "_~Error~_";
        private ReorderListItemLayoutType _layoutType = ReorderListItemLayoutType.Table;

       
        /// <summary>
        /// Specifies whether this control allows reorder.
        /// </summary>
        [DefaultValue(false)]
        public bool AllowReorder
        {
            get
            {
                return GetPropertyValue("AllowReorder", true);
            }
            set
            {
                SetPropertyValue("AllowReorder", value);
            }
        }

        private IOrderedDictionary BoundFieldValues
        {
            get
            {
                if (ViewState["BoundFieldValues"] == null)
                {
                    IOrderedDictionary bfv = new OrderedDictionary();
                    ViewState["BoundFieldValues"] = bfv;
                }
                return (IOrderedDictionary)ViewState["BoundFieldValues"];
            }
        }

        [DefaultValue("")]
        public string CallbackCssStyle
        {
            get
            {
                return GetPropertyValue("CallbackCssStyle", "");
            }
            set
            {
                SetPropertyValue("CallbackCssStyle", value);
            }
        }

        internal BulletedList ChildList
        {
            get
            {
                if (_childList == null)
                {
                    _childList = new BulletedList();
                    _childList.ID = "_rbl";
                    this.Controls.Add(_childList);
                }
                else if (_childList.Parent == null)
                {
                    // this gets cleared by base databinding code since the ChildList
                    // is parented to the ReorderList.
                    //
                    this.Controls.Add(_childList);                    
                }
                return _childList;
            }
        }

        /// <summary>
        /// The column name for the primary key field for this control to use
        /// </summary>
        [DefaultValue("")]
        public string DataKeyField
        {
            get
            {
                return GetPropertyValue("DataKeyName", "");
            }
            set
            {
                SetPropertyValue("DataKeyName", value);
            }
        }

        /// <summary>
        /// The indexed collection of data keys, one for each row, when databound.
        /// </summary>
        [Browsable(false)]
        public DataKeyCollection DataKeys
        {
            get
            {
                return new DataKeyCollection(DataKeysArray);
            }
        }

        /// <summary>
        /// Set to true when a reorder callback happens.  We check this on a 
        /// postback to see if we need to re-databind.        
        /// </summary>
        private bool DataBindPending
        {
            get
            {
                EnsureChildControls();
                if (_dropWatcherExtender != null)
                {
                    string state = _dropWatcherExtender.ClientState;
                    return !String.IsNullOrEmpty(state);
                }
                return false;
            }
        }

        protected ArrayList DataKeysArray
        {
            get
            {
                if (ViewState["DataKeysArray"] == null)
                {
                    ViewState["DataKeysArray"] = new ArrayList();
                }
                return (ArrayList)ViewState["DataKeysArray"];
            }
        }

        /// <summary>
        /// The ID of an IDataSource object to databind this list to.
        /// </summary>
        [TypeConverter(typeof(TypedControlIDConverter<IDataSource>))]
        public override string DataSourceID
        {
            get
            {
                return base.DataSourceID;
            }
            set
            {
                base.DataSourceID = value;
            }
        }

        /// <summary>
        /// The handle alignment in relation to the item template.  
        /// </summary>
        [DefaultValue(ReorderHandleAlignment.Left)]
        public ReorderHandleAlignment DragHandleAlignment
        {
            get
            {
                return GetPropertyValue("DragHandleAlignment", ReorderHandleAlignment.Left);
            }
            set
            {
                SetPropertyValue("DragHandleAlignment",value);
            }
        }


        /// <summary>
        /// The template to use for the handle that a user can "grab" with the mouse and reorder
        /// the item.
        /// </summary>
        [Browsable(false)]
        [TemplateContainer(typeof(ReorderListItem))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DefaultValue("")]        
        public ITemplate DragHandleTemplate
        {
            get { return _dragHandleTemplate; }
            set { _dragHandleTemplate = value; }
        }

        /// <summary>
        /// The template that will be shown when the list has no data
        /// </summary>
        [Browsable(false)]
        [TemplateContainer(typeof(ReorderListItem))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DefaultValue("")]
        public ITemplate EmptyListTemplate
        {
            get { return _emptyListTemplate; }
            set { _emptyListTemplate = value; }
        }

        /// <summary>
        /// The index of the item that is currently in edit mode.  
        /// The default is -1, meaning no item is in edit mode.
        /// </summary>
        [DefaultValue(-1)]
        public int EditItemIndex
        {
            get
            {
                return GetPropertyValue("EditItemIndex", -1);
            }
            set
            {
                SetPropertyValue("EditItemIndex", value);
            }
        }

        /// <summary>
        /// The template do display when a row is in edit mode.
        /// </summary>
        [Browsable(false)]
        [TemplateContainer(typeof(IDataItemContainer), BindingDirection.TwoWay)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DefaultValue("")]
        public ITemplate EditItemTemplate
        {
            get { return _editItemTemplate; }
            set { _editItemTemplate = value; }
        }

        /// <summary>
        /// Specifies where new items are added to the list: beginning or end.
        /// </summary>
        [DefaultValue(ReorderListInsertLocation.Beginning)]
        public ReorderListInsertLocation ItemInsertLocation
        {
            get
            {
                return GetPropertyValue("ItemInsertLocation", ReorderListInsertLocation.Beginning);
            }
            set
            {
                SetPropertyValue("ItemInsertLocation", value);
            }
        }
        
        /// <summary>
        /// The template to display for adding new items.
        /// </summary>
        [Browsable(false)]
        [TemplateContainer(typeof(IDataItemContainer), BindingDirection.TwoWay)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DefaultValue("")]
        public ITemplate InsertItemTemplate
        {
            get { return _insertItemTemplate; }
            set { _insertItemTemplate = value; }
        }        

        /// <summary>
        /// The template to display for rows in display mode.
        /// </summary>
        [Browsable(false)]

⌨️ 快捷键说明

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