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

📄 listpicker.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
📖 第 1 页 / 共 2 页
字号:

namespace ASPNET.StarterKit.Communities.Admin {


    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ASPNET.StarterKit.Communities;

   

    //*********************************************************************
    //
    // ListPicker Class
    //
    // Creates a server or client side list picker. 
    //
    //*********************************************************************
    
    public class ListPicker : WebControl, IPostBackDataHandler, INamingContainer {


        public event EventHandler ListChanged;

        private bool _isUplevel;
        private ArrayList _allItems = new ArrayList();
        private ArrayList _selectedItems = new ArrayList();
        private IEnumerable _dataSource  = null;
        private string _dataField = String.Empty;
        
        
        // The following controls are used by downlevel browsers
        ListBox lstAllItems;
        ListBox lstSelectedItems;
        Button btnAddItem;
        Button btnRemoveItem;
        
        

        //*********************************************************************
        //
        // Changed Property
        //
        // True when new item picked. 
        //
        //*********************************************************************
        public bool Changed {
            get {
                if (ViewState["Changed"] == null)
                    return false;
                else
                    return (bool)ViewState["Changed"];
            }
            
            set {
                ViewState["Changed"] = value;
            }
        }         
        
        

        public string SelectedItemsText {
            get { 
                if (ViewState["SelectedItemsText"] == null)
                    return "Selected Items";
                else
                    return (string)ViewState["SelectedItemsText"];
            }
            
            set { ViewState["SelectedItemsText"] = value; }
        }



        public string AllItemsText {
            get { 
                if (ViewState["AllItemsText"] == null)
                    return "All Items";
                else
                    return (string)ViewState["AllItemsText"];
            }
            
            set { ViewState["AllItemsText"] = value; }
        }



        //*********************************************************************
        //
        // Size Property
        //
        // Determines the number of items displayed by the picker.
        //
        //*********************************************************************
        public int Size {
            get {
                if (ViewState["Size"] == null)
                    return 5;
                else
                    return (int)ViewState["Size"];
            }
            set {
                ViewState["Size"] = value;
            }
        }



        //*********************************************************************
        //
        // SelectedState Property
        //
        // Preserves state between postbacks so change notification can be raised
        // only when state changes. 
        //
        //*********************************************************************
        private string SelectedState {
            get {  
                if (ViewState["selectedState"] == null)
                    return String.Empty;
                else
                    return (string)ViewState["selectedState"];
            }
            set { ViewState["selectedState"] = value; }
        }
        
        

        //*********************************************************************
        //
        // DataSource Property
        //
        // The Data Source for the All Items listbox
        //
        //*********************************************************************
        public IEnumerable DataSource { 
            get	{ return _dataSource; }
            set { _dataSource = value; }
        }  


        //*********************************************************************
        //
        // DataField Property
        //
        // The Data Field for the All Items Data Source
        //
        //*********************************************************************
        public string DataField { 
            get	{ return _dataField; }
            set { _dataField = value; }
        }  


        //*********************************************************************
        //
        // SelectedHelperID Property
        //
        // The ID of the hidden form field for the selected list box
        //
        //*********************************************************************
        protected string SelectedHelperID {
                get {return ClientID + "_SelectedState"; }
        }


        //*********************************************************************
        //
        // AllHelperID Property
        //
        // The ID of the hidden form field for the all list box
        //
        //*********************************************************************
        protected string AllHelperID {
                get {return ClientID + "_AllState"; }
        }


        //*********************************************************************
        //
        // OnInit Method
        //
        // Notifies framework that this control wants to be notified of data
        // changes on postback.
        //
        //*********************************************************************
        protected override void OnInit(EventArgs e) {
            if (Page != null) 
                Page.RegisterRequiresPostBack(this);
        }

        //*********************************************************************
        //
        // LoadPostData Method
        //
        // This method retrieves the posted data and update the control properties
        //
        //*********************************************************************
        public bool LoadPostData(String postDataKey, NameValueCollection values) {
            string _allState;
            string _selectedState;

            // return if null
            if (values[AllHelperID] == null)           
                return false;

            if (_isUplevel) {
                 _allState = values[AllHelperID].Trim();
                _selectedState = values[SelectedHelperID].Trim();
    
    
                if (_allState == String.Empty)
                    _allItems.Clear();
                else
                    _allItems = new ArrayList( _allState.Split(',') );
                
                if (_selectedState == String.Empty)
                    _selectedItems.Clear();
                else
                    _selectedItems = new ArrayList( _selectedState.Split(',') );

    
                // No change, return false
                if (SelectedState == _selectedState.Trim() )
                    return false;
    
                // Change, return true and update state
                Changed = true;
                SelectedState = _selectedState;
                return true;
            } else {
                if (Changed)
                    return true;
                else
                    return false;
            }
        }


        //*********************************************************************
        //
        // RaisePostDataChangedEvent Method
        //
        // This method raises the OnListChanged event if posted data changed
        //
        //*********************************************************************
        public void RaisePostDataChangedEvent() {
            OnListChanged(EventArgs.Empty);
        }       
        
 
        //*********************************************************************
        //
        // OnListChanged Method
        //
        // Method for handling list control post data changes
        //
        //*********************************************************************
        protected virtual void OnListChanged(EventArgs e) {
            if (ListChanged != null)
                ListChanged(this,e);
        }       


        //*********************************************************************
        //
        // OnDataBinding Method
        //
        // This method is called when the DataBind method is called
        //
        //*********************************************************************
        protected override void OnDataBinding( EventArgs e ) {
            IEnumerator objDataEnum;
 
            // bind all items
            if (_dataSource != null) {
                
                // Populate all items
                objDataEnum = _dataSource.GetEnumerator();
                while ( objDataEnum.MoveNext() )
                    if (_dataField == String.Empty)
                        _allItems.Add( (string)objDataEnum.Current );
                    else
                        _allItems.Add( (string)DataBinder.Eval(objDataEnum.Current, _dataField) );

            }

            
            // Remove selected items from all items
            foreach (string item in _selectedItems) {
                _allItems.Remove( item );
            }

            // Bind to ListBox for downlevel browsers
            if (! _isUplevel) {
                EnsureChildControls();

                lstAllItems.DataSource = _allItems;
                lstAllItems.DataBind();
                
                lstSelectedItems.DataSource = _selectedItems;
                lstSelectedItems.DataBind();
            }
        }        

⌨️ 快捷键说明

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