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

📄 duallist.cs

📁 实现DualList列表的Web自定义控件
💻 CS
📖 第 1 页 / 共 2 页
字号:
		DefaultValue("Items"),
		]
		public virtual String ItemsName {
			get {
				Object savedState = this.ViewState["ItemLabel"];
				if ( savedState != null ) {
					return (String)savedState;
				}
				return "Items";
			}
			set {
				this.ViewState["ItemLabel"] = value;
				this.EnsureChildControls();
				this.leftBoxLabel.Text = "Available " + value;
				this.rightBoxLabel.Text = "Chosen " + value;
			}
		}

		/// <summary>
		/// Gets or sets the visibility of the buttons for moving all items between the lists.
		/// </summary>
		[
		Description("Gets or sets the visibility of the buttons for moving all items between the lists."),
		Category("Behavior"),
		DefaultValue(false),
		]
		public virtual Boolean EnableMoveAll {
			get {
				Object savedState = this.ViewState["EnableMoveAll"];
				if ( savedState != null ) {
					return (Boolean)savedState;
				}
				return false;
			}
			set {
				this.ViewState["EnableMoveAll"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the visibility of the buttons for moving items up and down within the list on the right side of the control.
		/// </summary>
		[
		Description("Gets or sets the visibility of the buttons for moving items up and down within the RightBox."),
		Category("Behavior"),
		DefaultValue(false),
		]
		public virtual Boolean EnableMoveUpDown {
			get {
				Object savedState = this.ViewState["EnableMoveUpDown"];
				if ( savedState != null ) {
					return (Boolean)savedState;
				}
				return false;
			}
			set {
				this.ViewState["EnableMoveUpDown"] = value;
			}
		}

		/// <summary>
		/// Overrides <see cref="WebControl.Enabled"/>
		/// </summary>
		public override bool Enabled {
			get {
				return base.Enabled;
			}
			set {
				base.Enabled = value;
				this.EnsureChildControls();
				this.leftBox.Enabled = value;
				this.rightBox.Enabled = value;
				this.moveRight.Enabled = value;
				this.moveAllRight.Enabled = value;
				this.moveLeft.Enabled = value;
				this.moveAllLeft.Enabled = value;
				this.moveUp.Enabled = value;
				this.moveDown.Enabled = value;
			}
		}

		/// <summary>
		/// Gets the <see cref="WebControl.ControlStyle"/> of the buttons contained in the control.
		/// </summary>
		[
		Description("Gets the WebControl.ControlStyle of the buttons contained in the control."),
		Category("Appearance"),
		NotifyParentProperty(true),
		PersistenceMode(PersistenceMode.InnerProperty),
		]
		public virtual Style ButtonStyle {
			get {
				this.EnsureChildControls();
				return this.moveRight.ControlStyle;
			}
		}
		#endregion
		
		#region Life Cycle
		
		/// <summary>
		/// Initializes the contained controls.
		/// </summary>
		private void InitializeComponent() {
			// Instantiate
			this.leftBox = new DynamicListBox();
			this.rightBox = new DynamicListBox();
			this.moveRight = new Button();
			this.moveLeft = new Button();
			this.moveAllRight = new Button();
			this.moveAllLeft = new Button();
			this.moveUp = new Button();
			this.moveDown = new Button();
			this.leftBoxLabel = new Label();
			this.rightBoxLabel = new Label();
			this.allLeftContainer = new PlaceHolder();
			this.allRightContainer = new PlaceHolder();

			// Customize
			this.leftBox.ID = "LeftBox";
			this.leftBox.SelectionMode = ListSelectionMode.Multiple;
			this.leftBox.Rows = 8;
			this.leftBox.ItemsChanged += new EventHandler(leftBox_ItemsChanged);

			this.rightBox.ID = "RightBox";
			this.rightBox.SelectionMode = ListSelectionMode.Multiple;
			this.rightBox.Rows = 8;
			this.rightBox.ItemsChanged += new EventHandler(rightBox_ItemsChanged);

			this.moveRight.ID = "MoveRight";
			this.moveRight.Text = "Add ->";
			this.moveRight.CausesValidation = false;
			this.moveRight.Click += new EventHandler(moveRight_Click);

			this.moveAllRight.ID = "MoveAllRight";
			this.moveAllRight.Text = "Add All ->>";
			this.moveAllRight.CausesValidation = false;
			this.moveAllRight.Click += new EventHandler(moveAllRight_Click);

			this.moveLeft.ID = "MoveLeft";
			this.moveLeft.Text = "<- Remove";
			this.moveLeft.CausesValidation = false;
			this.moveLeft.Click += new EventHandler(moveLeft_Click);

			this.moveAllLeft.ID = "MoveAllLeft";
			this.moveAllLeft.Text = "<<- Remove All";
			this.moveAllLeft.CausesValidation = false;
			this.moveAllLeft.Click += new EventHandler(moveAllLeft_Click);

			this.moveUp.ID = "MoveUp";
			this.moveUp.Text = "Move Up";
			this.moveUp.CausesValidation = false;
			this.moveUp.Width = Unit.Parse("100%",System.Globalization.CultureInfo.InvariantCulture);
			this.moveUp.Click += new EventHandler(moveUp_Click);

			this.moveDown.ID = "MoveDown";
			this.moveDown.Text = "Move Down";
			this.moveDown.CausesValidation = false;
			this.moveDown.Click += new EventHandler(moveDown_Click);

			this.leftBoxLabel.Text = "Available " + this.ItemsName;
			this.rightBoxLabel.Text = "Chosen " + this.ItemsName;

			// Layout
			TableRow topRow = new TableRow();
			this.Controls.Add(topRow);
			topRow.Cells.AddRange(new TableCell[] { new TableCell(), new TableCell() } );
			topRow.Cells[0].ColumnSpan = 2;
			topRow.Cells[0].Controls.Add(this.leftBoxLabel);
			topRow.Cells[1].ColumnSpan = 2;
			topRow.Cells[1].Controls.Add(this.rightBoxLabel);

			TableRow mainRow = new TableRow();
			this.Controls.Add( mainRow );
			mainRow.Cells.AddRange( new TableCell[] { new TableCell(), new TableCell(), new TableCell(), new TableCell() } );
			
			TableCell currentCell;

			currentCell = mainRow.Cells[0];
			currentCell.Controls.Add(leftBox);
			currentCell.HorizontalAlign = HorizontalAlign.Center;
			
			currentCell = mainRow.Cells[1];
			currentCell.Controls.Add(moveRight);
			this.allRightContainer.Controls.Add(new LiteralControl("<br>"));
			this.allRightContainer.Controls.Add(moveAllRight);
			currentCell.Controls.Add(this.allRightContainer);
			currentCell.Controls.Add(new LiteralControl("<br>"));
			currentCell.Controls.Add(new LiteralControl("<br>"));
			currentCell.Controls.Add(moveLeft);
			this.allLeftContainer.Controls.Add(new LiteralControl("<br>"));
			this.allLeftContainer.Controls.Add(this.moveAllLeft);
			currentCell.Controls.Add(this.allLeftContainer);
			currentCell.HorizontalAlign = HorizontalAlign.Center;
			currentCell.VerticalAlign = VerticalAlign.Middle;
			
			currentCell = mainRow.Cells[2];
			currentCell.Controls.Add(rightBox);
			currentCell.HorizontalAlign = HorizontalAlign.Center;
			
			currentCell = mainRow.Cells[3];
			currentCell.Controls.Add(this.moveUp);
			currentCell.Controls.Add(new LiteralControl("<br>"));
			currentCell.Controls.Add(this.moveDown);
			currentCell.HorizontalAlign = HorizontalAlign.Left;
			currentCell.VerticalAlign = VerticalAlign.Middle;

		}

		/// <summary>
		/// Overrides <see cref="Control.DataBind"/>.
		/// </summary>
		public override void DataBind() {
			base.DataBind ();
			this.FixAvailableItems();
		}

		/// <summary>
		/// FixAvailableItems is called after <see cref="DataBind"/> to make sure that none of the items on the right, "chosen", list exist in the left, "available" list.
		/// </summary>
		protected virtual void FixAvailableItems() {
			foreach( ListItem item in this.rightBox.Items ) {
				ListItem leftItem = this.leftBox.Items.FindByValue(item.Value);
				if ( leftItem != null && leftItem.Text == item.Text ) {
					this.leftBox.Items.Remove(leftItem);
				}
			}
		}

		
		/// <summary>
		/// Overrides <see cref="Control.OnPreRender"/>.
		/// </summary>
		protected override void OnPreRender(EventArgs e) {
			base.OnPreRender (e);
			this.RegisterScript();
		}

		
		/// <summary>
		/// Overrides <see cref="Control.Render"/>.
		/// </summary>
		protected override void Render(HtmlTextWriter writer) {
			this.EnsureChildControls();
			this.allRightContainer.Visible = this.EnableMoveAll;
			this.allLeftContainer.Visible = this.EnableMoveAll;
			this.moveUp.Parent.Visible = this.EnableMoveUpDown;

			this.moveAllRight.ControlStyle.CopyFrom(this.moveRight.ControlStyle);
			this.moveLeft.ControlStyle.CopyFrom(this.moveRight.ControlStyle);
			this.moveAllLeft.ControlStyle.CopyFrom(this.moveRight.ControlStyle);
			this.moveUp.ControlStyle.CopyFrom(this.moveRight.ControlStyle);
			this.moveDown.ControlStyle.CopyFrom(this.moveRight.ControlStyle);

			base.Render (writer);
		}


		/// <summary>
		/// Overrides <see cref="WebControl.CreateControlStyle"/>.
		/// </summary>
		protected override Style CreateControlStyle() {
			return new TableStyle(this.ViewState);
		}

		
		#endregion

		#region ClientScript
		/// <summary>
		/// Registers the script for this control.
		/// </summary>
		protected virtual void RegisterScript() {
			if ( this.Page != null ) {
				this.RegisterScriptLibrary();
				this.RegisterScriptArray();
				this.RegisterScriptStartup();
			}
		}

		/// <summary>
		/// Registers the script library for this control.
		/// </summary>
		protected virtual void RegisterScriptLibrary() {
			DynamicListBoxResourceHandler.RegisterScript(this.Page,"MetaBuilders_DualList","DualList.js");
		}

		/// <summary>
		/// Registers the script array for this control.
		/// </summary>
		protected virtual void RegisterScriptArray() {
			this.EnsureChildControls();
			String idPrefix = this.UniqueID + this.leftBox.UniqueID.Substring( this.UniqueID.Length, 1 );
			this.Page.RegisterArrayDeclaration("MetaBuilders_DualLists","'" + idPrefix  + "'");
		}

		/// <summary>
		/// Registers the script which initializes this control.
		/// </summary>
		protected virtual void RegisterScriptStartup() {
			this.Page.RegisterStartupScript("MetaBuilders_DualList",@"
<script language='javascript'>
<!--
	MetaBuilders_DualList_Init();
//-->
</script>");
		}

		#endregion

		#region Child Control Event Handlers
		// These handlers will only fire if the client browser doesn't support clientscript

		private void moveRight_Click(object sender, EventArgs e) {
			this.MoveSelectedItems(this.leftBox,this.rightBox);
			this.EnsureChangeEvent();
		}

		private void moveAllRight_Click(object sender, EventArgs e) {
			this.MoveAllItems(this.leftBox,this.rightBox);
			this.EnsureChangeEvent();
		}

		private void moveLeft_Click(object sender, EventArgs e) {
			this.MoveSelectedItems(this.rightBox,this.leftBox);
			this.EnsureChangeEvent();
		}

		private void moveAllLeft_Click(object sender, EventArgs e) {
			this.MoveAllItems(this.rightBox,this.leftBox);
			this.EnsureChangeEvent();
		}

		private void moveUp_Click(object sender, EventArgs e) {
			Int32 originalIndex = this.rightBox.SelectedIndex;
			if ( originalIndex > 0 ) {
				ListItem movedItem = this.rightBox.SelectedItem;
				this.rightBox.Items.Remove(movedItem);
				this.rightBox.Items.Insert(originalIndex - 1, movedItem);
				this.EnsureChangeEvent();
			}
		}

		private void moveDown_Click(object sender, EventArgs e) {
			Int32 originalIndex = this.rightBox.SelectedIndex;
			if ( originalIndex < this.rightBox.Items.Count - 1 ) {
				ListItem movedItem = this.rightBox.SelectedItem;
				this.rightBox.Items.Remove(movedItem);
				this.rightBox.Items.Insert(originalIndex + 1, movedItem);
				this.EnsureChangeEvent();
			}
		}

		
		private void MoveSelectedItems(ListBox source, ListBox target) {
			while( source.SelectedIndex != -1 ) {
				target.Items.Add(source.SelectedItem);
				source.Items.Remove(source.SelectedItem);
			}
		}
		private void MoveAllItems(ListBox source, ListBox target) {
			while( source.Items.Count != 0 ) {
				target.Items.Add(source.Items[0]);
				source.Items.RemoveAt(0);
			}
		}

		private void leftBox_ItemsChanged(object sender, EventArgs e) {
			this.EnsureChangeEvent();
		}

		private void rightBox_ItemsChanged(object sender, EventArgs e) {
			this.EnsureChangeEvent();
		}
		private void EnsureChangeEvent() {
			if ( !this.hasNotifiedOfChange ) {
				hasNotifiedOfChange = true;
				this.OnItemsMoved(EventArgs.Empty);
			}
		}
		private Boolean hasNotifiedOfChange = false;
		#endregion

		#region Child Control References
		private DynamicListBox leftBox;
		private DynamicListBox rightBox;

		private Button moveRight;
		private Button moveLeft;
		private Button moveAllRight;
		private Button moveAllLeft;

		private Button moveUp;
		private Button moveDown;

		private Label leftBoxLabel;
		private Label rightBoxLabel;

		private PlaceHolder allRightContainer;
		private PlaceHolder allLeftContainer;
		#endregion
	}
}

⌨️ 快捷键说明

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