/// <summary>
        /// Sets the order string of each items..
        /// </summary>
        /// <param name="order">The order.</param>
        /// <param name="enumerate">if set to <c>true</c> enumerated each item.</param>
        public void SetOrderString(string order, bool enumerate)
        {
            this._items.Clear();

            System.Web.HttpContext.Current.Trace.Write(order);

            foreach (string item in order.Split('|'))
            {
                OrderedListBoxItem newItem = new OrderedListBoxItem();
                newItem.Value = item.Split('~')[0];

                System.Web.HttpContext.Current.Trace.Write(item.Split('~')[0]);
                System.Web.HttpContext.Current.Trace.Write(item.Split('~')[1]);
                System.Web.HttpContext.Current.Trace.Write(item.Split('~')[2]);

                string itemText = item.Split('~')[1];
                if (enumerate)
                {
                    newItem.Text = itemText.Substring(itemText.IndexOf(". ") + 1);
                }
                else
                {
                    newItem.Text = itemText;
                }
                newItem.Selected = Convert.ToBoolean(item.Split('~')[2]);
                this._items.Add(newItem);
            }
        }
        /// <summary>
        /// Determines the index of a specific <see cref="OrderedListBoxItem"/> in the current instance.
        /// </summary>
        /// <param name="item">The <see cref="OrderedListBoxItem"/> to locate in the current instance.</param>
        /// <returns>The index of value if found in the current instance; otherwise, -1.</returns>
        public int IndexOf(OrderedListBoxItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            return(_items.IndexOf(item));
        }
        /// <summary>
        /// Determines whether a <see cref="OrderedListBoxItem"/> is in the collection.
        /// </summary>
        /// <param name="item">The <see cref="OrderedListBoxItem"/> to locate in the collection. The element to locate can be a null reference.</param>
        /// <returns>true if value is found in the collection; otherwise, false.</returns>
        public bool Contains(OrderedListBoxItem item)
        {
            if (item == null)
            {
                return(false);
            }

            return(_items.Contains(item));
        }
        /// <summary>
        /// Move the selected item down.
        /// </summary>
        public void MoveSelectedDown()
        {
            int index = 0;

            for (index = _items.Count - 1; index >= 0; index--)
            {
                OrderedListBoxItem item = (OrderedListBoxItem)_items[index];

                if (item.Selected && !item.Locked)
                {
                    MoveDown(index);
                }
            }
        }
        /// <summary>
        /// Move the selected item up.
        /// </summary>
        public void MoveSelectedUp()
        {
            int index = 0;

            for (index = 0; index < _items.Count; index++)
            {
                OrderedListBoxItem item = (OrderedListBoxItem)_items[index];

                if (item.Selected && !item.Locked)
                {
                    MoveUp(index);
                }
            }
        }
        /// <summary>
        /// Adds a <see cref="OrderedListBoxItem"/> to the collection.
        /// </summary>
        /// <param name="item">The <see cref="OrderedListBoxItem"/> to add to the collection.</param>
        /// <returns>The position into which the new element was inserted.</returns>
        public int Add(OrderedListBoxItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (_isTrackingViewState)
            {
                ((IStateManager)item).TrackViewState();
                item.SetDirty();
            }

            return(_items.Add(item));
        }
        /// <summary>
        /// Inserts a <see cref="OrderedListBoxItem"/> to the collection at the specified position.
        /// </summary>
        /// <param name="index">The zero-based index at which value should be inserted.</param>
        /// <param name="item">The <see cref="OrderedListBoxItem"/> to insert into the Collection.</param>
        public void Insert(int index, OrderedListBoxItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            _items.Insert(index, item);

            if (_isTrackingViewState)
            {
                ((IStateManager)item).TrackViewState();
                _saveAll = true;
            }
        }
        /// <summary>
        /// When implemented by a class, saves the changes to a server control's view state to an
        /// <see cref="T:System.Object"/> .
        /// </summary>
        /// <returns>
        /// The <see langword="Object"/> that contains the view state changes.
        /// </returns>
        public object SaveViewState()
        {
            if (_saveAll == true)
            {
                // Save all items.
                ArrayList states = new ArrayList(Count);
                for (int i = 0; i < Count; i++)
                {
                    OrderedListBoxItem toolItem = (OrderedListBoxItem)_items[i];
                    toolItem.SetDirty();
                    states.Add(((IStateManager)toolItem).SaveViewState());
                }
                if (states.Count > 0)
                {
                    return(states);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                // Save only the dirty items.
                ArrayList indices = new ArrayList();
                ArrayList states  = new ArrayList();

                for (int i = 0; i < Count; i++)
                {
                    OrderedListBoxItem toolItem = (OrderedListBoxItem)_items[i];
                    object             state    = ((IStateManager)toolItem).SaveViewState();
                    if (state != null)
                    {
                        states.Add(state);
                        indices.Add(i);
                    }
                }

                if (indices.Count > 0)
                {
                    return(new Pair(indices, states));
                }
            }

            return(null);
        }
        /// <summary>
        /// Moves the selected item to list box.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="control">The control id.</param>
        public void MoveSelectedToListBox(System.Web.UI.Page page, string control)
        {
            // Get left listbox
            OrderedListBox listbox = (OrderedListBox)page.FindControl(control);

            int index = 0;

            for (index = _items.Count - 1; index >= 0; index--)
            {
                OrderedListBoxItem item = (OrderedListBoxItem)_items[index];

                if (item.Selected && !item.Locked)
                {
                    listbox.Items.Add(item);
                    _items.RemoveAt(index);
                }
            }
        }
        /// <summary>
        /// Removes the first occurrence of a specific <see cref="OrderedListBoxItem"/> from the collection.
        /// </summary>
        /// <param name="item">The <see cref="OrderedListBoxItem"/> to remove from the collection.</param>
        public void Remove(OrderedListBoxItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            int index = IndexOf(item);

            if (index >= 0)
            {
                RemoveAt(index);
            }

            if (_isTrackingViewState)
            {
                _saveAll = true;
            }
        }
        void IStateManager.LoadViewState(object savedState)
        {
            if (savedState == null)
            {
                return;
            }

            if (savedState is Pair)
            {
                Pair      p       = (Pair)savedState;
                ArrayList indices = (ArrayList)p.First;
                ArrayList states  = (ArrayList)p.Second;

                for (int i = 0; i < indices.Count; i++)
                {
                    int index = (int)indices[i];
                    if (index < this.Count)
                    {
                        ((IStateManager)_items[index]).LoadViewState(states[i]);
                    }
                    else
                    {
                        OrderedListBoxItem toolItem = new OrderedListBoxItem();
                        Add(toolItem);
                        ((IStateManager)toolItem).LoadViewState(states[i]);
                    }
                }
            }
            else if (savedState is ArrayList)
            {
                _saveAll = true;
                ArrayList states = (ArrayList)savedState;

                _items = new ArrayList(states.Count);
                for (int i = 0; i < states.Count; i++)
                {
                    OrderedListBoxItem toolItem = new OrderedListBoxItem();
                    Add(toolItem);
                    ((IStateManager)toolItem).LoadViewState(states[i]);
                }
            }
        }
        /// <summary>
        /// Raises the DataBinding event.
        /// </summary>
        /// <param name="e">An EventArgs object that contains the event data.</param>
        protected override void OnDataBinding(EventArgs e)
        {
            Page.Trace.Write("OnDataBinding");
            base.OnDataBinding(e);

            string dataTextField     = DataTextField;
            string dataValueField    = DataValueField;
            string dataSelectedField = DataSelectedField;
            string dataLockedField   = DataLockedField;
            string dataTextFormat    = DataTextFormatString;
            bool   valuePresent      = false;
            bool   formatPresent     = false;

            IEnumerable iEnumerable = Utils.GetResolvedDataSource(DataSource, DataMember);

            if (iEnumerable == null)
            {
                return;
            }

            Items.Clear();

            ICollection iCollection = iEnumerable as ICollection;

            if ((dataTextField.Length != 0) || (dataValueField.Length != 0))
            {
                valuePresent = true;
            }
            if (dataTextFormat.Length != 0)
            {
                formatPresent = true;
            }

            IEnumerator iEnumerator = iEnumerable.GetEnumerator();

            try
            {
                while (iEnumerator.MoveNext())
                {
                    object             current  = iEnumerator.Current;
                    OrderedListBoxItem toolItem = new OrderedListBoxItem();
                    if (valuePresent)
                    {
                        if (dataTextField.Length > 0)
                        {
                            toolItem.Text = DataBinder.GetPropertyValue(current, dataTextField, dataTextFormat);
                        }
                        if (dataValueField.Length > 0)
                        {
                            toolItem.Value = DataBinder.GetPropertyValue(current, dataValueField, null);
                        }
                        if (dataSelectedField.Length > 0)
                        {
                            toolItem.Selected = (bool)DataBinder.GetPropertyValue(current, dataSelectedField);
                        }
                        if (dataLockedField.Length > 0)
                        {
                            toolItem.Locked = (bool)DataBinder.GetPropertyValue(current, dataLockedField);
                        }
                    }
                    else
                    {
                        if (formatPresent)
                        {
                            toolItem.Text = string.Format(dataTextFormat, current);
                        }
                        else
                        {
                            toolItem.Text = current.ToString();
                        }
                    }

                    Items.Add(toolItem);
                }
            }

            finally
            {
                IDisposable iDisposable = iEnumerator as IDisposable;
                if (iDisposable != null)
                {
                    iDisposable.Dispose();
                }
            }
        }