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

📄 combobox.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;


namespace WebCombobox {
	/// <summary>
	/// Use the ComboBox control to create a web control that behaves like a text box and select control. Specify items listings in the control by placing HTML &lt;option&gt; elements between the opening and closing &lt;tns:combobox&gt; tags. Each item is represented by a System.Web.UI.WebControls.ListItem.
	/// Unlike the HtmlSelect control, the ListItem.Value property cannot be used to associate a different value to the combox box.
	/// The Size property specifices the width of the text box in number of characters, much like the HtmlInputText control. If the size is not set, the width of the control will accommodate the largest ListItem.Text property value.
	/// The ComboBox Control implements databiding. The DataSource property specifies the data source to bind to. Use the DataMember property if you're DataSource impements ISourceList, such as the DataSet object, to specify which data member you want bound.
	/// The DataTextField is used to associate the ListItem.Text property.
	/// </summary>
	/// 
	[
	ControlBuilderAttribute(typeof(ComboBoxBuilder)),
	ParseChildren(false),
	ToolboxData(@"<{0}:ComboBox runat=server><asp:ListItem>ComboBox</asp:ListItem></{0}:ComboBox>"),
	ToolboxBitmap(typeof(WebCombobox.ComboBox)),
	Designer(typeof(WebCombobox.Design.ComboBoxDesigner)),
	]
	public class ComboBox: WebControl, IPostBackDataHandler {
		/// <summary>
		/// Initializes a new instance of the ComboBox class.
		/// </summary>
		public ComboBox() {
			EventServerChange = new Object();
		}
		#region Private Members
		private ListItemCollection items;
		private int cachedSelectedIndex;
		private string _value = String.Empty;
		private int _size = 20;
		private object _datasource = null;
		#endregion

		private object EventServerChange;

		/// <summary>
		/// The ServerChange event is raised when the selected items in the ComboBox control change between posts to the server.
		/// </summary>
		public event EventHandler ServerChange {
			add {this.Events.AddHandler(this.EventServerChange, value);}
			remove {this.Events.RemoveHandler(this.EventServerChange, value);}
		}

		#region Public Properties

		/// <summary>
		/// Gets a collection that contains the items listed in an ComboBox control.
		/// </summary>
		/// 
		[
		DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
		PersistenceMode(PersistenceMode.InnerDefaultProperty),
		Editor(typeof(System.Web.UI.Design.WebControls.ListItemsCollectionEditor), typeof(System.Drawing.Design.UITypeEditor)),
		]
		public ListItemCollection Items {
			get {
				if (this.items == null) {
					this.items = new ListItemCollection();
				}
				return this.items;
			}
		}
		/// <summary>
		/// Gets or sets the zero-based index of the selected tab.
		/// The index is based on the order of list items.
		/// The default is -1, which indicates that nothing is selected 
		/// or the text value does not match any list items.
		/// </summary>
		/// 
		[Browsable(false)]
		public int SelectedIndex {
			get {
				for (int i=0; i< this.Items.Count; i++) {
					if (this.Items[i].Value.Equals(this.Value))
						return i;
				}
				return -1;
			}
			set {
				if (this.Items.Count == 0) {
					this.cachedSelectedIndex = value;
					return;
				}
				if (value < -1 || value >= this.Items.Count)
					throw new ArgumentOutOfRangeException("value");
				this.ClearSelection();
				if (value >= 0) {
					this.Items[value].Selected = true;
					_value = this.Items[value].Value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the text value.  Also sets the selected index 
		/// if text value matches any list item in the drop down list
		/// </summary>
		/// 
		[Category("Behavior")]
		public string Value {
			get {
				string sValue = this.Attributes["value"];
				if (sValue == null) {
					return String.Empty; 
				}
				return sValue;
			}
			set {
				this.Attributes["value"] = value;
				int index = this.Items.IndexOf(this.Items.FindByText(value));
				if (index >= 0) {
					this.SelectedIndex = index;
				}
				else
					this.SelectedIndex = -1;
			}
		}

		/// <summary>
		/// Gets or sets the width size of the text field. 
		/// Defaults to match the size of dropdown list.
		/// </summary>
		/// 
		[Category("Appearance")]
		public int Size {
			get {return _size;}
			set {
				_size = value;
				this.Attributes["size"] = _size.ToString();
			}
		}


		/// <summary>
		/// Gets or sets the error message alerted on the client when validation failes of the text field. 
		/// </summary>
		[Category("Behavior")]
		public string ErrorMessage {
			get {
				string retval = this.Attributes["ErrorMessage"];
				if (retval != null)
					return (string) retval;
				else
					return String.Empty;
			}
			set {
				this.Attributes["ErrorMessage"] = value;
			}
		}


		/// <summary>
		/// Gets or sets a value indicating whether validate text field against the list item values. 
		/// Default value is false;
		/// </summary>
		[Category("Behavior")]
		public bool AutoValidate {
			get {
				Object retval = ViewState["AutoValidate"];
				return ((retval == null) ? false : (bool)retval);
			}
			set {
				ViewState["AutoValidate"] = value;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether an automatic postback to the server 
		/// will occur whenever the user changes the selected index.
		/// </summary>
		/// 
		[Category("Behavior")]
		public bool AutoPostBack {
			get {
				Object obj = ViewState["AutoPostBack"];
				return ((obj == null) ? false : (bool)obj);
			}
			set { ViewState["AutoPostBack"] = value; }
		}

		/// <summary>
		/// Gets or sets the source of information to bind to the ComboBox control.
		/// </summary>
		/// 
		[
		Bindable(true),
		Category("Data"),
		DefaultValue(null),
		Description("The datasource that is used to populate the list items."),
		// needs to be hidden otherwise we don't save the property for some reason
		DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
		]
		public object DataSource {
			get {return _datasource;}
			set {
				if (value == null || ((value is IListSource) || (value is IEnumerable))) {
					_datasource = value;
					return; 
				}
				throw new ArgumentException("An invalid data source is being used for " + base.ID + ". A valid data source must implement either IListSource or IEnumerable. "+(value is System.Data.DataView));
			}
		}
		/// <summary>
		/// Gets or sets the field from the DataSource to bind to the ListItem.Text 
		/// property of each item in the ComboBox control.
		/// </summary>
		/// 
		[
		Bindable(false),
		Category("Data"),
		DefaultValue(""),
		Description("The field in the DataSource that provides the list item's text.")
		]
		public string DataTextField {
			get {
				object retval = ViewState["DataTextField"];
				if (retval != null) {
					return (string) retval;
				}
				return string.Empty;
			}
			set {this.ViewState["DataTextField"] = value;}
		}

		/// <summary>
		/// Gets or sets the field from the DataSource to bind to the ListItem.Value 
		/// property of each item in the ComboBox control.
		/// </summary>
		/// 
		[
		Bindable(false),
		Category("Data"),
		DefaultValue(""),
		Description("The field in the DataSource that provides the list item's value.")
		]
		public string DataValueField {
			get {
				object retval = ViewState["DataValueField"];
				if (retval != null) {
					return (string) ViewState["DataValueField"];
				}
				return string.Empty;
			}
			set {ViewState["DataValueField"] = value;}
		}
		/// <summary>
		/// Gets or sets the set of data to bind to the ComboBox control from a DataSource with multiple sets of data
		/// </summary>
		/// 
		[
		Bindable(false),
		Category("Data"),
		DefaultValue(""),
		Description("The table used for binding when a DataSet is used as the DataSource.")
		]
		public string DataMember {
			get {
				object retval = ViewState["DataMember"];
				if (retval != null) {
					return ((string) retval); 
				}
				return string.Empty;
			}
			set { ViewState["DataMember"] = value;}
		}
		#endregion
		
		#region Public Constants
		/// <summary>
		/// The namespace for the ComboBox and its children.
		/// </summary>
		public const string TagNamespace = "CBNS";

		/// <summary>
		/// The ComboBox's tag name.
		/// </summary>
		public const string ComboBoxTagName = "ComboBox";

		/// <summary>
		/// The ComboBox default client location
		/// </summary>
		public const string DefaultCommonFilesRoot = "/webctrl_client/progstudios/";
		#endregion

		#region Internal Properties
		/// <summary>
		/// Client-side script ID of the hidden helper.
		/// </summary>

⌨️ 快捷键说明

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