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

📄 checkboxlist.cs

📁 不错的人事管理系统
💻 CS
字号:
/*
 *
导读: 
	 1、CheckBoxList是从ListControl中派生的,
		其派生关系是 object --> Control --> WebControl --> ListControl --> CheckBoxList
		
	 2、ListBox重载了Render方法,但最终实现输出HTML是通过RepeatInfo来实现,
		具体实现方式请察看RepeatInfo的代码
		
	3、CheckBoxList输出的HTML其实是一个CheckBox和一个Lable的组合的集合
	
	4、通过CheckBoxList可以传统的使用多个Name属性相同<input type="checkbox">的情况
 * 
 */
using System;
using System.Collections.Specialized;

namespace System.Web.UI.WebControls
{
	/// <summary>
	/// Summary description for CheckBoxList.
	/// </summary>
	public class CheckBoxList : ListControl, 
		System.Web.UI.WebControls.IRepeatInfoUser,
		 System.Web.UI.INamingContainer,
		System.Web.UI.IPostBackDataHandler
	{
		private CheckBox controlToRepeat;
		private bool hasNotifiedOfChange;

		public CheckBoxList() : base() 
		{
			this.controlToRepeat = new CheckBox();
			this.controlToRepeat.ID = "0";
			this.controlToRepeat.EnableViewState = false;
			this.Controls.Add(this.controlToRepeat);
			this.hasNotifiedOfChange = false;
		}

		protected override Style CreateControlStyle() 
		{
			return new TableStyle(this.ViewState);
		}

		protected override Control FindControl(string id, int pathOffset) 
		{
			return this;
		}

		//通知Page,所有的Items都需要回传数据
		protected override void OnPreRender(EventArgs e) //ok
		{
			this.controlToRepeat.AutoPostBack = this.AutoPostBack;

			if (this.Page != null) 
			{
				for(int i=0; i<this.Items.Count; i++)
				{
					if (this.Items[i].Selected) 
					{
						this.controlToRepeat.ID = i.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
						this.Page.RegisterRequiresPostBack(this.controlToRepeat);
					}
				}
			}
		}
		
		protected override void Render(System.Web.UI.HtmlTextWriter writer) //ok
		{

			RepeatInfo local0;
			Style local1;
			short local2;
			bool local3;

			local0 = new RepeatInfo();
			
			
			local1 = (this.ControlStyleCreated)? this.ControlStyle : null;
			local2 = this.TabIndex;
			local3 = false;
			this.controlToRepeat.TabIndex = local2;
			if (local2 != 0) 
			{
				if (!(this.ViewState.IsItemDirty("TabIndex")))
					local3 = true;
				this.TabIndex = 0;
			}

			//
			local0.RepeatColumns = this.RepeatColumns;
			local0.RepeatDirection = this.RepeatDirection;
			local0.RepeatLayout = this.RepeatLayout;

			//在这里调用RepeatInfo的RenderRepeater方法输出HTML
			local0.RenderRepeater(writer, this, local1, this);
			
			if (local2 != 0)
			{
				this.TabIndex = local2;
			}

			//在已经保存的ViewState中设置TabIndex项的值
			if (local3)
			{
				this.ViewState.SetItemDirty("TabIndex", false);
			}
		}


		bool System.Web.UI.IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection) //ok
		{
			string local0;
			int local1;
			bool local2;

			local0 = postDataKey.Substring(this.UniqueID.Length + 1);
			local1 = int.Parse(local0);
			if((local1 >= 0) 
				&& (local1 < this.Items.Count) 
				)
			{
				local2 = (postCollection[postDataKey] != null);

				if (this.Items[local1].Selected != local2) 
				{
					this.Items[local1].Selected = local2;

					if (!(this.hasNotifiedOfChange)) 
					{
						this.hasNotifiedOfChange = true;
						return true;
					}
				}
			}
			return false;
		}

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

		bool System.Web.UI.WebControls.IRepeatInfoUser.HasHeader
		{
			get
			{
				return false;
			}
		}

		bool System.Web.UI.WebControls.IRepeatInfoUser.HasFooter
		{
			get
			{
				return false;
			}
		}

		bool System.Web.UI.WebControls.IRepeatInfoUser.HasSeparators
		{
			get
			{
				return false;
			}
		}

		int System.Web.UI.WebControls.IRepeatInfoUser.RepeatedItemCount
		{
			get
			{
				return this.Items.Count;
			}
		}

		Style System.Web.UI.WebControls.IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex) 
		{
			return null;
		}

		void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, System.Web.UI.HtmlTextWriter writer) 
		{
			this.controlToRepeat.ID = repeatIndex.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
			this.controlToRepeat.Text = this.Items[repeatIndex].Text;
			this.controlToRepeat.TextAlign = this.TextAlign;
			this.controlToRepeat.Checked = this.Items[repeatIndex].Selected;
			this.controlToRepeat.RenderControl(writer);
		}

		public virtual int CellPadding
		{
			get
			{
				if (!(this.ControlStyleCreated))
					return -1;
				return ((TableStyle)this.ControlStyle).CellPadding;
			}
			set
			{
				((TableStyle) this.ControlStyle).CellPadding = value;
			}
		}

		public virtual int CellSpacing
		{
			get
			{
				if (!(this.ControlStyleCreated))
					return -1;
				return ((TableStyle)this.ControlStyle).CellSpacing;
			}
			set
			{
				((TableStyle)this.ControlStyle).CellSpacing = value;
			}
		}

		public virtual int RepeatColumns
		{
			get
			{
				object local0;

				local0 = this.ViewState["RepeatColumns"];
				if (local0 != null)
					return (Int32) local0;
				return 0;
			}
			set
			{
				if (value < 0)
					throw new ArgumentOutOfRangeException("value");
				this.ViewState["RepeatColumns"] = value;
			}
		}

		public virtual RepeatDirection RepeatDirection
		{
			get
			{
				object local0;

				local0 = this.ViewState["RepeatDirection"];
				if (local0 != null)
					return (RepeatDirection) local0;

				return RepeatDirection.Vertical;
			}
			set
			{
				if ((value != RepeatDirection.Horizontal) && (value != RepeatDirection.Vertical))
					throw new ArgumentOutOfRangeException("value");

				this.ViewState["RepeatDirection"] = value;
			}
		}

		public virtual RepeatLayout RepeatLayout
		{
			get
			{
				object local0;

				local0 = this.ViewState["RepeatLayout"];
				if (local0 != null)
					return (RepeatLayout) local0;
				return 0;
			}
			set
			{
				if ((value != RepeatLayout.Table) && (value != RepeatLayout.Flow))
					throw new ArgumentOutOfRangeException("value");
				this.ViewState["RepeatLayout"] = value;

			}
		}

		public virtual TextAlign TextAlign
		{
			get
			{
				object local0;

				local0 = this.ViewState["TextAlign"];
				if (local0 != null)
					return (TextAlign) local0;
				return TextAlign.Right;
			}
			set
			{
				if((value != TextAlign.Left) && (value != TextAlign.Right))
					throw new ArgumentOutOfRangeException("value");
				this.ViewState["TextAlign"] = value;
			}
		}

	}
}

⌨️ 快捷键说明

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