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

📄 1.txt

📁 如何给用户控件定义属性 前言 ASP.NET的用户控件功能很强大
💻 TXT
字号:
namespace DotNet
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;



	/// <summary>
	///  MyControl 的摘要说明。
	/// </summary>
	public class MyControl : System.Web.UI.UserControl
	{
  
		protected System.Web.UI.WebControls.DropDownList Drop;
		protected System.Web.UI.WebControls.CheckBoxList Check;
		protected System.Web.UI.WebControls.RadioButtonList Radio;
		protected System.Web.UI.WebControls.Button btnGetValue;
		protected System.Web.UI.WebControls.Label lbMsg;



  
 
		private ViewTypes _MyTypes;
		private string _MyValue = ;
			private int _MyMaxNumber=0;
		private int _MyMinNumber=0;
		//我们定义的属性,_MyTypes枚举值,_MyMaxNumber,_MyMinNumber为3个控件绑定的最小值,和最大值.
		public string MyValue //这里暂时不会用到
		{
  
			get 
			{ 
				return _MyValue;
			}
			set 
			{
				_MyValue = value; 
			}
		}
		public enum ViewTypes//定义3个控件的枚举值
		{
			Drop,
			Check,
			Radio
		}
		public ViewTypes MyTypes
		{
			get
			{
				return _MyTypes;
			}
			set
			{
				_MyTypes= value;
			}
		}
		public int MyMaxNumber
		{
			get 
			{
				return _MyMaxNumber;
			}
			set 
			{
				_MyMaxNumber=value;
			}
		}
		public int MyMinNumber
		{
			get 
			{
				return _MyMinNumber;
			}
			set 
			{
				_MyMinNumber=value;
			}
		}



		/*
		 以上MyValue,MyTypes,MyMaxNumber,MyMinNumber四个模块都是给控件定义的四个属性,在这里面get{}部分在这里有读取值的作用,而set{}部分是往属性里面写值,。注意:他们前面的要用public定义,绝对不能用private,protected定义这里涉及到一个见级性问题,不明白的朋友请参阅相关书籍,还一个要注意的地方是我们定义的属性值不能和上面声明的变量名一样。例如:
		private int _intCount;
		public int _intCount//切忌,这里千万不要和上面声明的变量是一样的,在这里是错误的,要改成public int intCount
		{
		 get{return _intCount;}
		 set{_intCount=value;}
		}
		在这里有个技巧,再声明变量的时候,可以给第一个字母加上下划线,或者再定义属性的时候的时候第一个字母大写,比如private int intCount,然后就是public int IntCount。
		 我们在来说一下这几个属性是干什么用的:MyTypes是显示哪种控件(在这里定义了一个枚举,里面有三个值,分别指的是下拉列表框,复选框,和单选按钮,别忘了在上面声明哦 private ViewTypes _MyTypes);MyMaxNumber,MyMinNumber是绑定到控件上最大值和最小值;
		*/
 



		private void Page_Load(object sender, System.EventArgs e)
		{
			this.GetControlInformation();//调用函数
		}
		private void GetControlInformation()
		{
  
			if(!Page.IsPostBack)
				for(int intCount=this._MyMinNumber;intCount<=this._MyMaxNumber;intCount++)
					//迭代绑定控件,_MyMinNumber为起始值,_MyMaxNumber为最大值
				{
     
     
					switch(this._MyTypes)//判断枚举值
					{
						case ViewTypes.Drop://显示下拉列表,绑定值,以下都是一样的...
							this.Drop.Visible=true;
							this.Drop.Items.Add(intCount.ToString());      
							break;
						case ViewTypes.Check:
							this.Check.Visible=true;
							this.Check.RepeatColumns=this._MyMaxNumber;
							this.Check.Items.Add(intCount.ToString());      
							break;
						case ViewTypes.Radio:
							this.Radio.Visible=true;
							this.Radio.RepeatColumns=this._MyMaxNumber;
							this.Radio.Items.Add(intCount.ToString());      
							break;
     
					}
				}



		}
		//上面三个控件的Visible属性都设置为false,




		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
  
		/// <summary>
		///  设计器支持所需的方法 - 不要使用代码编辑器
		///  修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnGetValue.Click += new System.EventHandler(this.btnGetValue_Click);
			this.Load += new System.EventHandler(this.Page_Load);



		}
		#endregion



		private void btnGetValue_Click(object sender, System.EventArgs e)
		{
			string str=;
				if(this._MyTypes==ViewTypes.Drop)
				{
					str=下拉列表框;
				}
				else if(this._MyTypes==ViewTypes.Check)
				{
					str=复选框;
				}
				else if(this._MyTypes==ViewTypes.Radio)
				{
					str=单选按钮;
				}
			this.lbMsg.Text=控件类型:+str+ 绑定数据最小值:+this.MyMinNumber.ToString()+最大值:+this.MyMaxNumber.ToString();
		}



 



		/*
		<%@ Register  TagPrefix=UserControl alt=如何给用户控件定义属性(C#:ASP.NET) src=http://www.3pcode.com/article/article_76/MyControl.ascx  TagName=MyControl  %>
		<UserControl:MyControl id=con runat=server MyTypes=Radio MyMinNumber=1 MyMaxNumber=10></UserControl:MyControl>
		按钮的功能是在标签控件上显示相关信息,选择哪种类型,和哪个控件绑定的最大值和最小值,行了到现在你就可以调用这个控件了,当然程序的健壮性还得有所改善,在这里只是起到抛砖引玉的作用。大家有什么不会的就到http://www.cs-aspx.net去发帖子,我尽力回答大家的问题!!!!!
		*/



	}
}

⌨️ 快捷键说明

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