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

📄 webcustomcontrol1.cs

📁 CSDN上的朋友
💻 CS
字号:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;

namespace LzhTextBpx
{		
	public class LabelTextbox : System.Web.UI.WebControls.WebControl
	{
		private Label label = new Label();//创建一个标签
		private TextBox textBox = new TextBox();//创建一个文本框

		private Color _backgroundColor;//鼠标移入的背景颜色

		private const string MOUSE_SCRIPT = //用来触发鼠标事件的脚本
			"<script language=javascript>\n" +
					"var OldColor;\n" + 
			"function ltmouseover(ctrl,color)\n" + //当鼠标移入时,调用该方法,ctrl为表格,color为要改变的颜色值
			"{\n" +
				"OldColor = ctrl.style.backgroundColor;\n" + //记录下原来的文档背景颜色
				"ctrl.style.color = '#ffffff';\n" +//将字体颜色改为白色
				"ctrl.style.backgroundColor = color;\n" + //更改表格背景颜色
			"}\n" +
			"function ltmouseout(ctrl)\n" + //鼠标移出时调用 ,参数同上
			"{\n" +
				"ctrl.style.backgroundColor = OldColor;\n" + //还原背景颜色
				"ctrl.style.color = '#000000';" + //将字体颜色还原成黑色
			"}\n" +
			"</script>\n";

		public Color backgroundColor
		{
			get
			{
				if(_backgroundColor == Color.Empty)
					return Color.Blue;
				return _backgroundColor;
			}
			set
			{
				_backgroundColor = value;
			}
		}

		[CategoryAttribute("自定义的复杂类型设置(包括自定义类型转换器)"),
		TypeConverterAttribute(typeof(TextConverter)),
		ReadOnlyAttribute(false)]
		public string labelString
		{
			get
			{
				return label.Text;
			}
			set
			{
				label.Text = value;
			}
		}

		public string textString
		{
			get
			{
				return textBox.Text;
			}
			set
			{
				textBox.Text = value;
			}
		}

		public LabelTextbox()
		{
		}

		private void CreateControls()//创建控件以及设置控件的相关属性
		{
			//以下将颜色值转化成十六进制表示
			string R,G,B;
			R = (Convert.ToInt32(this._backgroundColor.R)).ToString("X");
			G = (Convert.ToInt32(this._backgroundColor.G)).ToString("X");
			B = (Convert.ToInt32(this._backgroundColor.B)).ToString("X");
			//定义一个表对象
			Table t = new Table();

			//添加鼠标事件
			t.Attributes.Add("onmouseover","ltmouseover(this,'" + "#" + R + G + B + "')");
			t.Attributes.Add("onmouseout","ltmouseout(this)");

			//添加样式,用来控制字体
			t.Style.Add("font-size","10pt");

			//添加一行
			TableRow tr = new TableRow();
			//添加两个单元格
			TableCell tc1 = new TableCell();
			TableCell tc2 = new TableCell();

			//将控件添加到Controls集中.
			tc1.Controls.Add(label);
			tc2.Controls.Add(textBox);
			tr.Controls.Add(tc1);
			tr.Controls.Add(tc2);

			t.Controls.Add(tr);

            this.Controls.Add(t);			
		}

		protected override void CreateChildControls()
		{
			this.EnsureChildControls();//如果子控件没有创建,则创建
			base.CreateChildControls ();//调用方法
			CreateControls();
		}

		
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			this.EnsureChildControls();
			base.AddAttributesToRender (writer);
			writer.AddStyleAttribute("font-size","9pt");
		}

		protected override void OnPreRender(EventArgs e)
		{
			//将脚本输出到页面中.
			if(!Page.IsClientScriptBlockRegistered("mousescript")) //防止重复输出.
			{
				Page.RegisterClientScriptBlock("mousescript",MOUSE_SCRIPT);
			}
			base.OnPreRender (e);
		}

	}

	class TextConverter : System.ComponentModel.StringConverter
	{
		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
		{
			return new StandardValuesCollection(new string[]{"李赞红","小船","小弟"});
		}

		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
		{
			return false;
		}
	}


}

⌨️ 快捷键说明

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