using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Web.UI.Design; using System.Drawing.Design; using System.Drawing; namespace RD.WebControls { [DefaultProperty( " LeftListDataTextField " )] [ToolboxData( " <{0}:TwoListBox runat=server></{0}:TwoListBox> " )] public class TwoListBox : CompositeControl { Private Member #region Private Member private ListBox m_LeftListBox; private ListBox m_RightListBox; private System.Web.UI.WebControls.Image m_ImageRemoveLeft; private System.Web.UI.WebControls.Image m_ImageRemoveRight; private System.Web.UI.WebControls.Image m_ImageRemoveAllLeft; private System.Web.UI.WebControls.Image m_ImageRemoveAllRight; private System.Web.UI.WebControls.Image m_ImageRemoveUp; private System.Web.UI.WebControls.Image m_ImageRemoveDown; private System.Web.UI.WebControls.Image m_ImageRemoveTop; private System.Web.UI.WebControls.Image m_ImageRemoveBottom; private System.Web.UI.WebControls.Label m_LeftLable; private System.Web.UI.WebControls.Label m_RightLable; private TextBox m_LeftHiddenTextBox; private TextBox m_RightHiddenTextBox; private string strLeftImageUrl = string .Empty ; private string strLeftssImageUrl = string .Empty; private string strRightImageUrl = string .Empty; private string strRightssImageUrl = string .Empty; private string strUpImageUrl = string .Empty; private string strDownImageUrl = string .Empty; private string strTopImageUrl = string .Empty; private string strBottomImageUrl = string .Empty; #endregion Public property #region Public property [DefaultValue(" LeftListTitle " ), Category( " TwoListBox " ), Description( " 左邊文字方塊Text " ), Localizable( true )] public string LeftListTitle { get { object obj = ViewState[ClientID + " LeftListTitle " ]; return (obj == null ) ? " LeftListTitle " : ( string )obj; } set { ViewState[ClientID + " LeftListTitle " ] = value; } } [DefaultValue(" RightListTitle " ), Category( " TwoListBox " ), Description( " 右邊文字方塊Text " ), Localizable( true )] public string RightListTitle { get { object obj = ViewState[ClientID + " RightListTitle " ]; return (obj == null ) ? " RightListTitle " : ( string )obj; } set { ViewState[ClientID + " RightListTitle " ] = value; } } [DefaultValue(100 ), Category( " TwoListBox " ), Description( " ListBox寬度 " ), Localizable( true )] public Unit ListBoxWidth { get { object obj = ViewState[ClientID + " ListBoxWidth " ]; return (obj == null ) ? 100 : (Unit)obj; } set { ViewState[ClientID + " ListBoxWidth " ] = value; } } [Category(" TwoListBox " ), Description( " ListBox高度 " ), Localizable( true )] public Unit ListBoxHeight { get { object obj = ViewState[ClientID + " ListBoxHeight " ]; return (obj == null ) ? Unit.Empty : (Unit)obj; } set { ViewState[ClientID + " ListBoxHeight " ] = value; } } [Category(" TwoListBox " ), Description( " ListBox是否允許多選 " ), Localizable( true )] public ListSelectionMode SelectionMode { get { object obj = ViewState[ClientID + " SelectionMode " ]; return (obj == null ) ? ListSelectionMode.Single : (ListSelectionMode)obj; } set { ViewState[ClientID + " SelectionMode " ] = value; } } [Category(" TwoListBox " ), Description( " 右邊所有移動圖片按鈕是否可見 " ), Localizable( true )] public bool IsRightImageVisable { get { object obj = ViewState[ClientID + " IsRightImageVisable " ]; return (obj == null ) ? true : ( bool )obj; } set { ViewState[ClientID + " IsRightImageVisable " ] = value; } } [Category(" TwoListBox " ), Description( " 左邊ListBox的DataValueField " ), Localizable( true )] public string LeftListDataValueField { get { object obj = this .ViewState[ " LeftListDataValueField " ]; return (obj == null ) ? string .Empty : ( string )obj; } set { this .ViewState[ " LeftListDataValueField " ] = value; } } [Category(" TwoListBox " ), Description( " 左邊ListBox的DataTextField " ), Localizable( true )] public string LeftListDataTextField { get { object obj = this .ViewState[ " LeftListDataTextField " ]; return (obj == null ) ? string .Empty : ( string )obj; } set { this .ViewState[ " LeftListDataTextField " ] = value; } } [Category(" TwoListBox " ), Description( " 右邊ListBox的DataValueField " ), Localizable( true )] public string RightListDataValueField { get { object obj = this .ViewState[ " RightListDataValueField " ]; return (obj == null ) ? string .Empty : ( string )obj; } set { EnsureChildControls(); this .ViewState[ " RightListDataValueField " ] = value; } } [Category(" TwoListBox " ), Description( " 右邊ListBox的DataTextField " ), Localizable( true )] public string RightListDataTextField { get { object obj = this .ViewState[ " RightListDataTextField " ]; return (obj == null ) ? string .Empty : ( string )obj; } set { EnsureChildControls(); this .ViewState[ " RightListDataTextField " ] = value; } } [Category(" TwoListBox " ), Description( " LeftListBox資料源 " ), Localizable( true ), Browsable( false )] public object LeftListDataSource { get { object obj = ViewState[ClientID + " LeftListDataSource " ]; return (obj == null ) ? null : obj; } set { EnsureChildControls(); ViewState[ClientID + " LeftListDataSource " ] = value; } } [Category(" TwoListBox " ), Description( " RightListBox資料源 " ), Localizable( true ), Browsable( false )] public object RightListDataSource { get { object obj = ViewState[ClientID + " RightListDataSource " ]; return (obj == null ) ? null : obj; } set { EnsureChildControls(); ViewState[ClientID + " RightListDataSource " ] = value; } } [Category(" TwoListBox " ), Description( " LeftListBox資料集合 " ), Localizable( true ), Browsable( false )] public ListItemCollection LeftListBoxItems { get { EnsureChildControls(); return (m_LeftListBox == null ) ? null : m_LeftListBox.Items; } } [Category(" TwoListBox " ), Description( " RightListBox資料集合 " ), Localizable( true ), Browsable( false )] public ListItemCollection RightListBoxItems { get { EnsureChildControls(); return (m_RightListBox == null ) ? null : m_RightListBox.Items; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 左移按鈕圖片URl " )] public string IamgeUrlLeft { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlLeft " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlLeft " ] = value; strLeftImageUrl = value; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 右移按鈕圖片URl " )] public string IamgeUrlRight { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlRight " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlRight " ] = value; strRightImageUrl = value; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 左移全部按鈕圖片URl " )] public string IamgeUrlLeftss { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlLeftss " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlLeftss " ] = value; strLeftssImageUrl = value; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 右移全部按鈕圖片URl " )] public string IamgeUrlRightss { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlRightss " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlRightss " ] = value; strRightImageUrl = value; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 上移按鈕圖片URl " )] public string IamgeUrlUp { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlLeftUp " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlLeftUp " ] = value; strUpImageUrl = value; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 下移按鈕圖片URl " )] public string IamgeUrlDown { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlDown " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlDown " ] = value; strDownImageUrl = value; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 上移到頂全部按鈕圖片URl " )] public string IamgeUrlTop { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlTop " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlTop " ] = value; strTopImageUrl = value; } } [Category(" TwoListBox " ), Editor( " System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a " , typeof (UITypeEditor)), DefaultValue( "" ), UrlProperty, Bindable( true ), Description( " 下移到底全部按鈕圖片URl " )] public string IamgeUrlBottom { get { string text = ( string ) this .ViewState[ClientID + " IamgeUrlBottom " ]; if (text != null ) { return text; } return string .Empty; } set { ViewState[ClientID + " IamgeUrlBottom " ] = value; strBottomImageUrl = value; } } #endregion Override Function #region Override Function protected override void OnInit(EventArgs e) { if ( this .Page != null ) { Page.ClientScript.RegisterClientScriptResource( this .GetType(), " RD.WebControls.TwoListBox.TwoListBox.js " ); base .OnInit(e); } } protected override void OnLoad(EventArgs e) { base .OnLoad(e); if (Page.IsPostBack) { string strLeftTextValue = m_LeftHiddenTextBox.Text; string strRightTextValue = m_RightHiddenTextBox.Text; LeftListDataSource = ConcertHiddenTextBoxToTable(strLeftTextValue, LeftListDataTextField, LeftListDataValueField); RightListDataSource = ConcertHiddenTextBoxToTable(strRightTextValue,RightListDataTextField, RightListDataValueField); } DataBindLeftListBox(); DataBindRightListBox(); } protected override void RecreateChildControls() { EnsureChildControls(); } protected override void CreateChildControls() { Controls.Clear(); m_LeftListBox = new ListBox(); m_LeftListBox.ID = " LeftListBox " ; m_RightListBox = new ListBox(); m_RightListBox.ID = " RightListBox " ; m_ImageRemoveLeft = new System.Web.UI.WebControls.Image(); m_ImageRemoveLeft.ID = " ImageRemoveLeft " ; m_ImageRemoveRight = new System.Web.UI.WebControls.Image(); m_ImageRemoveRight.ID = " ImageRemoveRight " ; m_ImageRemoveAllLeft = new System.Web.UI.WebControls.Image(); m_ImageRemoveAllLeft.ID = " ImageRemoveAllLeft " ; m_ImageRemoveAllRight = new System.Web.UI.WebControls.Image(); m_ImageRemoveAllRight.ID = " ImageRemoveAllRight " ; m_ImageRemoveUp = new System.Web.UI.WebControls.Image(); m_ImageRemoveUp.ID = " ImageRemoveUp " ; m_ImageRemoveDown = new System.Web.UI.WebControls.Image(); m_ImageRemoveDown.ID = " ImageRemoveDown " ; m_ImageRemoveTop = new System.Web.UI.WebControls.Image(); m_ImageRemoveTop.ID = " ImageRemoveTop " ; m_ImageRemoveBottom = new System.Web.UI.WebControls.Image(); m_ImageRemoveBottom.ID = " ImageRemoveBottom " ; m_LeftLable = new Label(); m_LeftLable.ID = " LeftLable " ; m_RightLable = new Label(); m_RightLable.ID = " RightLable " ; m_LeftHiddenTextBox = new TextBox(); m_LeftHiddenTextBox.ID = " LeftHiddenTextBox " ; m_RightHiddenTextBox = new TextBox(); m_RightHiddenTextBox.ID = " RightHiddenTextBox " ; this .Controls.Add(m_LeftListBox); this .Controls.Add(m_RightListBox); this .Controls.Add(m_ImageRemoveLeft); this .Controls.Add(m_ImageRemoveRight); this .Controls.Add(m_ImageRemoveAllLeft); this .Controls.Add(m_ImageRemoveAllRight); this .Controls.Add(m_ImageRemoveUp); this .Controls.Add(m_ImageRemoveDown); this .Controls.Add(m_ImageRemoveTop); this .Controls.Add(m_ImageRemoveBottom); this .Controls.Add(m_LeftLable); this .Controls.Add(m_RightLable); this .Controls.Add(m_LeftHiddenTextBox); this .Controls.Add(m_RightHiddenTextBox); } protected override void Render(HtmlTextWriter writer) { EnsureChildControls(); SetProptyToRender(); AddAttributesToRender(writer); writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, " 0px " ); writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, " 0px " ); writer.RenderBeginTag(HtmlTextWriterTag.Table); writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); m_LeftLable.RenderControl(writer); writer.RenderEndTag(); // Td writer.RenderBeginTag(HtmlTextWriterTag.Td); m_LeftHiddenTextBox.RenderControl(writer); writer.RenderEndTag(); // Td writer.RenderBeginTag(HtmlTextWriterTag.Td); m_RightLable.RenderControl(writer); writer.RenderEndTag(); // Td writer.RenderBeginTag(HtmlTextWriterTag.Td); m_RightHiddenTextBox.RenderControl(writer); writer.RenderEndTag(); // Td writer.RenderEndTag(); // Tr writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, " 4 " ); writer.RenderBeginTag(HtmlTextWriterTag.Td); m_LeftListBox.RenderControl(writer); writer.RenderEndTag(); // Td writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveRight.RenderControl(writer); writer.RenderEndTag(); // Td writer.AddAttribute(HtmlTextWriterAttribute.Rowspan, " 4 " ); if (IsRightImageVisable) { writer.RenderBeginTag(HtmlTextWriterTag.Td); m_RightListBox.RenderControl(writer); writer.RenderEndTag(); // Td writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveTop.RenderControl(writer); writer.RenderEndTag(); // Td } else { writer.AddAttribute(HtmlTextWriterAttribute.Colspan, " 2 " ); writer.RenderBeginTag(HtmlTextWriterTag.Td); m_RightListBox.RenderControl(writer); writer.RenderEndTag(); // Td } writer.RenderEndTag(); // Tr writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveAllRight.RenderControl(writer); writer.RenderEndTag(); // Td if (IsRightImageVisable) { writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveUp.RenderControl(writer); writer.RenderEndTag(); // Td } writer.RenderEndTag(); // Tr writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveAllLeft.RenderControl(writer); writer.RenderEndTag(); // Td if (IsRightImageVisable) { writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveDown.RenderControl(writer); writer.RenderEndTag(); // Td } writer.RenderEndTag(); // Tr writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveLeft.RenderControl(writer); writer.RenderEndTag(); // Td if (IsRightImageVisable) { writer.RenderBeginTag(HtmlTextWriterTag.Td); m_ImageRemoveBottom.RenderControl(writer); writer.RenderEndTag(); // Td } writer.RenderEndTag(); // Tr writer.RenderEndTag(); // Table } #endregion Public Function #region Public Function #endregion Private Function #region Private Function /**/ /// <summary> /// 在Render之前為子控件添加屬性 /// </summary> private void SetProptyToRender() { m_LeftLable.Text = LeftListTitle; m_RightLable.Text = RightListTitle; m_LeftHiddenTextBox.Width = 0 ; m_RightHiddenTextBox.Width = 0 ; m_LeftHiddenTextBox.Style.Add( " display " , " none " ); m_RightHiddenTextBox.Style.Add( " display " , " none " ); if (IamgeUrlLeft.Length == 0 ) strLeftImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.left.gif " ); if (IamgeUrlLeftss.Length == 0 ) strLeftssImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.leftss.gif " ); if (IamgeUrlRight.Length == 0 ) strRightImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.right.gif " ); if (IamgeUrlRightss.Length == 0 ) strRightssImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.rightss.gif " ); if (IamgeUrlUp.Length == 0 ) strUpImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.up.gif " ); if (IamgeUrlDown.Length == 0 ) strDownImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.down.gif " ); if (IamgeUrlTop.Length == 0 ) strTopImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.top.gif " ); if (IamgeUrlBottom.Length == 0 ) strBottomImageUrl = Page.ClientScript.GetWebResourceUrl( this .GetType(), " RD.WebControls.TwoListBox.bottom.gif " ); m_ImageRemoveLeft.ImageUrl = strLeftImageUrl; m_ImageRemoveRight.ImageUrl = strRightImageUrl; m_ImageRemoveAllLeft.ImageUrl = strLeftssImageUrl; m_ImageRemoveAllRight.ImageUrl = strRightssImageUrl; m_ImageRemoveUp.ImageUrl = strUpImageUrl; m_ImageRemoveDown.ImageUrl = strDownImageUrl; m_ImageRemoveTop.ImageUrl = strTopImageUrl; m_ImageRemoveBottom.ImageUrl = strBottomImageUrl; m_ImageRemoveLeft.Attributes.Add( " alt " , " 左移 " ); m_ImageRemoveRight.Attributes.Add( " alt " , " 右移 " ); m_ImageRemoveAllLeft.Attributes.Add( " alt " , " 左移全部 " ); m_ImageRemoveAllRight.Attributes.Add( " alt " , " 右移全部 " ); m_ImageRemoveUp.Attributes.Add( " alt " , " 上移 " ); m_ImageRemoveDown.Attributes.Add( " alt " , " 下移 " ); m_ImageRemoveTop.Attributes.Add( " alt " , " 上移到頂 " ); m_ImageRemoveBottom.Attributes.Add( " alt " , " 下移到底 " ); m_ImageRemoveLeft.Attributes.Add( " onclick " , " RemoveLeft() " ); m_ImageRemoveRight.Attributes.Add( " onclick " , " RemoveRight() " ); m_ImageRemoveAllLeft.Attributes.Add( " onclick " , " RemoveAllLeft() " ); m_ImageRemoveAllRight.Attributes.Add( " onclick " , " RemoveAllRight() " ); m_ImageRemoveUp.Attributes.Add( " onclick " , " RemoveUp() " ); m_ImageRemoveDown.Attributes.Add( " onclick " , " RemoveDown() " ); m_ImageRemoveTop.Attributes.Add( " onclick " , " RemoveTop() " ); m_ImageRemoveBottom.Attributes.Add( " onclick " , " RemoveBottom() " ); m_LeftListBox.SelectionMode = SelectionMode; m_RightListBox.SelectionMode = SelectionMode; m_LeftListBox.Width = ListBoxWidth; m_RightListBox.Width = ListBoxWidth; m_LeftListBox.Height = ListBoxHeight; m_RightListBox.Height = ListBoxHeight; this .Width = Unit.Empty; this .Height = Unit.Empty; } /**/ /// <summary> /// 用指定的資料源綁定左邊ListBox /// </summary> private void DataBindLeftListBox() { try { m_LeftListBox.DataTextField = LeftListDataTextField; m_LeftListBox.DataValueField = LeftListDataValueField; m_LeftListBox.DataSource = LeftListDataSource; m_LeftListBox.DataBind(); } catch { } } /**/ /// <summary> /// 用指定的資料源綁定右邊的ListBox /// </summary> private void DataBindRightListBox() { try { m_RightListBox.DataTextField = RightListDataTextField; m_RightListBox.DataValueField = RightListDataValueField; m_RightListBox.DataSource = RightListDataSource; m_RightListBox.DataBind(); } catch { } } // 將隱藏欄位的值拆分轉換為數據Table private DataTable ConcertHiddenTextBoxToTable( string strTextValue, string strTextField, string strValueField) { DataTable o_DataTable = new DataTable(); o_DataTable.Columns.Add(strTextField); o_DataTable.Columns.Add(strValueField); string [] ListBoxTextValue = strTextValue.Split( ' # ' ); for ( int i = 0 ; i < ListBoxTextValue.Length && ListBoxTextValue.Length > 1 ; i += 2 ) { DataRow o_DataRow = o_DataTable.NewRow(); o_DataRow[ 0 ] = ListBoxTextValue[i]; o_DataRow[ 1 ] = ListBoxTextValue[i + 1 ]; o_DataTable.Rows.Add(o_DataRow); } return o_DataTable; } #endregion } }
TwoListBox.js文件
// 左移動所選中的ListBox中的值 function RemoveLeft() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var LeftListBox = document.getElementById(ClentID + " _LeftListBox " ); var RightListBox = document.getElementById(ClentID + " _RightListBox " ); for ( var i = 0 ;i < RightListBox.options.length;i ++ ) { if (RightListBox.options[i].selected == true ) { var NewOption = document.createElement( " option " ); NewOption.value = RightListBox.options[i].value; NewOption.text = RightListBox.options[i].text; LeftListBox.options.add(NewOption); RightListBox.options.remove(i); i -- ; } } GetListBoxValueToHiden(ClentID); return false ; } // 右移動所選中的ListBox中的值 function RemoveRight() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var LeftListBox = document.getElementById(ClentID + " _LeftListBox " ); var RightListBox = document.getElementById(ClentID + " _RightListBox " ); for ( var i = 0 ;i < LeftListBox.options.length;i ++ ) { if (LeftListBox.options[i].selected == true ) { var NewOption = document.createElement( " option " ); NewOption.value = LeftListBox.options[i].value; NewOption.text = LeftListBox.options[i].text; RightListBox.options.add(NewOption); LeftListBox.options.remove(i); i -- ; } } GetListBoxValueToHiden(ClentID); return false ; } // 左移動ListBox中全部的值 function RemoveAllLeft() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var LeftListBox = document.getElementById(ClentID + " _LeftListBox " ); var RightListBox = document.getElementById(ClentID + " _RightListBox " ); for ( var i = 0 ;i < RightListBox.options.length;) { var NewOption = document.createElement( " option " ); NewOption.value = RightListBox.options[i].value; NewOption.text = RightListBox.options[i].text; LeftListBox.options.add(NewOption); RightListBox.options.remove(i); } GetListBoxValueToHiden(ClentID); return false ; } // 右移動ListBox中全部的值 function RemoveAllRight() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var LeftListBox = document.getElementById(ClentID + " _LeftListBox " ); var RightListBox = document.getElementById(ClentID + " _RightListBox " ); for ( var i = 0 ;i < LeftListBox.options.length;) { var NewOption = document.createElement( " option " ); NewOption.value = LeftListBox.options[i].value; NewOption.text = LeftListBox.options[i].text; RightListBox.options.add(NewOption); LeftListBox.options.remove(i); } GetListBoxValueToHiden(ClentID); return false ; } // 上移動右邊ListBox中選中的值 function RemoveUp() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var RightListBox = document.getElementById(ClentID + " _RightListBox " ); for ( var i = 1 ;i < RightListBox.options.length;i ++ ) { if (RightListBox.options[i].selected == true ) { var strSelectText = RightListBox.options[i].text; var strSelectValue = RightListBox.options[i].value; RightListBox.options[i].text = RightListBox.options[i - 1 ].text; RightListBox.options[i].value = RightListBox.options[i - 1 ].value; RightListBox.options[i - 1 ].text = strSelectText; RightListBox.options[i - 1 ].value = strSelectValue; RightListBox.options[i].selected = false ; RightListBox.options[i - 1 ].selected = true ; } } GetListBoxValueToHiden(ClentID); return false ; } // 下移動右邊ListBox中選中的值 function RemoveDown() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var RightListBox = document.getElementById(ClentID + " _RightListBox " ); for ( var i = RightListBox.options.length - 2 ;i >= 0 ;i -- ) { if (RightListBox.options[i].selected == true ) { var strSelectText = RightListBox.options[i].text; var strSelectValue = RightListBox.options[i].value; RightListBox.options[i].text = RightListBox.options[i + 1 ].text; RightListBox.options[i].value = RightListBox.options[i + 1 ].value; RightListBox.options[i + 1 ].text = strSelectText; RightListBox.options[i + 1 ].value = strSelectValue; RightListBox.options[i].selected = false ; RightListBox.options[i + 1 ].selected = true ; } } GetListBoxValueToHiden(ClentID); return false ; } // 上移到頂端右邊ListBox中選中的值 function RemoveTop() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var RightListBox = document.getElementById(ClentID + " _RightListBox " ); var first = RightListBox.firstChild; for ( var i = 1 ;i < RightListBox.options.length;i ++ ) { if (RightListBox.options[i].selected == true ) { var NewOption = RightListBox.removeChild(RightListBox.options[i]); RightListBox.insertBefore(NewOption,first); RightListBox.options[ 0 ].selected = true ; } } GetListBoxValueToHiden(ClentID); return false ; } // 上移到頂端右邊ListBox中選中的值 function RemoveBottom() { var curElement = event.srcElement; var ClentID = curElement.parentNode.parentNode.parentNode.parentNode.id; var RightListBox = document.getElementById(ClentID + " _RightListBox " ); // 循環計算選中的個數 var SelectLength = 0 ; for ( var i = 0 ;i < RightListBox.options.length;i ++ ) { if (RightListBox.options[i].selected == true ) SelectLength ++ ; } // 順序移動到底端 for ( var i = 0 ;i < RightListBox.options.length;i ++ ) { if (RightListBox.options[i].selected == true ) { var NewOption = RightListBox.removeChild(RightListBox.options[i]); i -- ; NewOption.selected = false ; RightListBox.appendChild(NewOption); } } // 設置選擇 for ( var i = RightListBox.options.length - 1 ;i >= RightListBox.options.length - SelectLength;i -- ) { RightListBox.options[i].selected = true ; } GetListBoxValueToHiden(ClentID); return false ; } // 把當前2個ListBox中的值存入2個隱藏文本框中 function GetListBoxValueToHiden(ClentID) { var LeftListBox = document.getElementById(ClentID + " _LeftListBox " ); var RightListBox = document.getElementById(ClentID + " _RightListBox " ); var LeftListBoxValue = "" ; var RigthListBoxValue = "" ; for ( var i = 0 ;i < LeftListBox.options.length;i ++ ) { LeftListBoxValue += LeftListBox.options[i].text + " # " + LeftListBox.options[i].value + " # " ; } for ( var j = 0 ;j < RightListBox.options.length;j ++ ) { RigthListBoxValue += RightListBox.options[j].text + " # " + RightListBox.options[j].value + " # " ; } if (LeftListBoxValue.length > 0 ) LeftListBoxValue = LeftListBoxValue.substring( 0 ,LeftListBoxValue.length - 1 ); if (RigthListBoxValue.length > 0 ) RigthListBoxValue = RigthListBoxValue.substring( 0 ,RigthListBoxValue.length - 1 ); document.getElementById(ClentID + " _LeftHiddenTextBox " ).value = LeftListBoxValue; document.getElementById(ClentID + " _RightHiddenTextBox " ).value = RigthListBoxValue; }