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

📄 listbox.cs

📁 不错的人事管理系统
💻 CS
字号:
/*
 *
导读: 
	 1、ListBox是从ListControl中派生的,
		其派生关系是 object --> Control --> WebControl --> ListControl --> ListBox
		
	 2、ListBox实现了,System.Web.UI.IPostBackDataHandler,
		在其Items的Selected状态改变时,响应SelectedIndexChanged事件
		
	 3、如果AutoPostBack属性值为True,客户端发生Onchange事件时,进行事件的回传,
		具体实现在AddAttributesToRender中
		
	 4、ListBox其实就是实现了允许多选的DropDownList
 * 
 */
using System;
using System.Collections;

namespace System.Web.UI.WebControls
{
	/// <summary>
	/// Summary description for ListBox.
	/// </summary>
	public class ListBox : ListControl, 
		System.Web.UI.IPostBackDataHandler
	{
		public ListBox()
		{
		}

		protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer) 
		{
			if (this.Page != null)
			{
				this.Page.VerifyRenderingInServerForm(this);
			}

			writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Name, this.UniqueID);
			
			base.AddAttributesToRender(writer);

			writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Size, this.Rows.ToString(System.Globalization.NumberFormatInfo.InvariantInfo));
			
			if (this.SelectionMode == ListSelectionMode.Multiple)
			{
				writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Multiple, "multiple");
			}

			if (this.AutoPostBack && this.Page != null) 
			{
				//客户端回传事件的实现
				writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Onchange, this.Page.GetPostBackClientEvent(this, ""));
				writer.AddAttribute("language", "javascript");
			}
		}

		public virtual int Rows
		{
			get
			{
				object local0;

				local0 = this.ViewState["Rows"];
				if (local0 != null)
					return (Int32) local0;
				return 4;
			}
		}

		public virtual ListSelectionMode SelectionMode
		{
			get
			{
				object local0;

				local0 = this.ViewState["SelectionMode"];

				if (local0 != null)
				{
					return (ListSelectionMode) local0;
				}
				return ListSelectionMode.Single;
			}
		}


		protected override void OnPreRender(EventArgs e) 
		{
			this.OnPreRender(e);
			if((this.Page != null) 
				&& (this.SelectionMode == ListSelectionMode.Multiple)
				&& (this.Enabled)
				)
			{
				this.Page.RegisterRequiresPostBack(this);
			}
		}


		protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) 
		{
			bool local0;
			bool local1;
			ListItemCollection local2;
			int local3;
			int local4;
			ListItem local5;

			local0 = false;
			local1 = this.SelectionMode == 0;
			local2 = this.Items;
			local3 = local2.Count;
			if (local3 > 0) 
			{
				local4 = 0;
				while (local4 < local3) 
				{
					local5 = local2[local4];
					writer.WriteBeginTag("option");
					if (local5.Selected) 
					{
						if (local1) 
						{
							if (local0)
								throw new HttpException(System.Web.HttpRuntime.FormatResourceString("Cant_Multiselect_In_Single_Mode"));
							local0 = true;
						}
						writer.WriteAttribute("selected", "selected");
					}
					writer.WriteAttribute("value", local5.Value, true);
					writer.Write('>');
					System.Web.HttpUtility.HtmlEncode(local5.Text, writer);
					writer.WriteEndTag("option");
					writer.WriteLine();
					local4++;
				}
			}
		}

		//私有实现System.Web.UI.IPostBackDataHandler接口的LoadPostData方法
		//如果返回值为true,RaisePostDataChangedEvent将会被调用
		bool System.Web.UI.IPostBackDataHandler.LoadPostData(string postDataKey, 
			System.Collections.Specialized.NameValueCollection postCollection)  //ok
		{
			string[] local0;
			bool local1;
			int local2;
			int local3;
			ArrayList local4;
			ArrayList local5;
			int local6;
			int local7;
			int local8;

			local0 = postCollection.GetValues(postDataKey);
			local1 = false;

			//判断Items的Selected的状态是否被改变
			if (local0 != null) //else goto 00db
			{
				//SelectionMode为Single时的判断
				if(this.SelectionMode == ListSelectionMode.Single) //else goto 0041
				{
					local2 = this.Items.FindByValueInternal(local0[0]);
					if(local2 != this.SelectedIndex) //else goto 00ed
					{
						this.SelectedIndex = local2; 
						local1 = true; //goto 00ed
					}
				}
				else //SelectionMode为Multiple时的判断
				{
					//line 0041
					local3 = (int) local0.Length;
					local4 = this.SelectedIndicesInternal;
					local5 = new ArrayList(local3);

					for(local6 = 0;local6 < local3; local6++)
					{
						local5.Add(this.Items.FindByValueInternal(local0[local6]));
					}

					local7 = 0;
					if (local4 != null)
					{
						local7 = local4.Count;
					}

					if (local7 == local3) 
					{
						for(local8 = 0; local8 < local3; local8++)
						{
							if ((int) local5[local8] != (int) local4[local8]) 
							{
								local1 = true;
								break;
							}
						}
					}
					else
					{
						local1 = true;
					}

					if(local1)
					{
						this.SelectInternal(local5);
					}
				}

			}
			else if (this.SelectedIndex != -1) 
			{
				this.SelectedIndex = -1;
				local1 = true;
			}

			//line 00ed
			return local1;
		}

		void System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() 
		{
			this.OnSelectedIndexChanged(System.EventArgs.Empty);
		}

		
		public override string ToolTip
		{
			get
			{
				return System.String.Empty;
			}
			set
			{
			}
		}

		public override System.Web.UI.WebControls.BorderStyle BorderStyle
		{
			get
			{
				return base.BorderStyle;
			}
			set
			{
				base.BorderStyle = value;
			}
		}

		public override System.Web.UI.WebControls.Unit BorderWidth
		{
			get
			{
				return base.BorderWidth;
			}
			set
			{
				base.BorderWidth = value;
			}
		}

		public override System.Drawing.Color BorderColor
		{
			get
			{
				return base.BorderColor;
			}
			set
			{
				base.BorderColor = value;
			}
		}

	}
}

⌨️ 快捷键说明

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