using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
namespace Component_Test
{
/// <summary>
/// LabelTextBox 的摘要说明。
/// </summary>
public class LabelTextBox: System.Web.UI.WebControls.WebControl
{
private Label label = new Label();//创建一个标签
private TextBox textBox = new TextBox();//创建一个文本框
private Color focus_forecolor = Color.White ;//得到焦点的前景颜色
private Color blur_forecolor = Color.Black ;//得到焦点的前景颜色
private Color focus_backcolor = Color.Blue ;//得到焦点的背景颜色
private Color blur_backcolor = Color.White ;//得到焦点的背景颜色
private Unit label_width;
private Unit textbox_width;
private Font label_font;
private Font textbox_font;
private const string MOUSE_SCRIPT = //用来触发事件的脚本
"<script language=javascript>/n" +
" function LTFocus(ctrl,ffc,fbc)/n" + //当控件得到焦点时,调用该方法,ctrl为表格
" {/n" +
" ctrl.style.color = ffc;/n" +
" ctrl.style.backgroundColor = fbc;/n" +
" }/n" +
" function LTBlur(ctrl,bfc,bbc)/n" +
" {/n" +
" ctrl.style.color = bfc;/n" +
" ctrl.style.backgroundColor = bbc;/n" +
" }/n" +
"</script>/n";
protected override void OnPreRender(EventArgs e)
{
//将脚本输出到页面中.
if(!Page.IsClientScriptBlockRegistered("mousescript")) //防止重复输出.
{
Page.RegisterClientScriptBlock("mousescript",MOUSE_SCRIPT);
}
base.OnPreRender (e);
}
protected override void Render(HtmlTextWriter writer)
{
this.EnsureChildControls ();
base.Render (writer);
}
//其中focus_backcolor是自定义属性。
//这一步的全部代码如下:
private void CreateControls()//创建控件以及设置控件的相关属性
{
//定义一个表对象
Table t = new Table();
t.ID = this.ID + "_table";
//添加鼠标事件
this.textBox .Attributes.Add("onfocus","LTFocus("+t.ID+",'" + this.focus_forecolor.Name + "','" + this.focus_backcolor.Name + "')");
this.textBox .Attributes.Add("onblur","LTBlur("+t.ID+",'"
+ this.blur_forecolor.Name + "','" + this.blur_backcolor.Name + "')");
//添加样式,用来控制字体
t.Attributes .Add("onfocus","LTFocus("+t.ID+",'" + this.focus_forecolor.Name + "','" + this.focus_backcolor.Name + "')");
t.Attributes .Add("onblur","LTBlur("+t.ID+",'" + this.blur_forecolor.Name + "','" + this.blur_backcolor.Name + "')");
t.Style.Add("font-size","12px");
//添加一行
TableRow tr = new TableRow();
//添加两个单元格
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
tc1.Style.Add ("width","50px");
tc2.Style.Add ("width",((double)(this.textBox.Width .Value + 10)).ToString ());
//将控件添加到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);
}
public Color focus_BackColor
{
get{return focus_backcolor;}
set{focus_backcolor = value;}
}
public Color blur_BackColor
{
get{return this.blur_backcolor;}
set{this.blur_backcolor = value;}
}
public Color focus_ForeColor
{
get{return this.focus_forecolor;}
set{this.focus_forecolor = value;}
}
public Color blur_ForeColor
{
get{return this.blur_forecolor;}
set{this.blur_forecolor = value;}
}
public Unit Label_Width
{
get{return this.label_width;}
set{this.label_width = value;}
}
public Unit TextBox_Width
{
get{return this.textbox_width;}
set{this.textbox_width = value;}
}
public Font Label_Font
{
get{return this.label_font ;}
set{this.label_font = value;}
}
public Font TextBox_Font
{
get{return this.textbox_font ;}
set{this.textbox_font = value;}
}
public string LabelString
{
get{return label.Text;}
set{label.Text = value;}
}
public string TextBoxString
{
get{return textBox.Text;}
set{textBox.Text = value;}
}
//这一步需要重写 CreateChildControls() 方法,该方法会自动被调用,用来生成子控件。
protected override void CreateChildControls()
{
this.EnsureChildControls();//如果子控件没有创建,则创建
base.CreateChildControls ();//调用方法
CreateControls();
}
}
}