ASP.NET组建(1) ASP.net 组合控件 Label TextBox

原创 2006年05月31日 16:20:00

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();

         }

     }

}

 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

asp.net的textbox获取时间控件(javascript)(1)

在ASP.NET前台加上这么两句: 下面的代码写在一个叫WebCalendar.js名字的js脚本里 var cal; var isFocus=false; //是否为焦...

ASP.Net TextBox控件只允许输入数字

1.1、在Asp.Net TextBox 控件的 OnKeyPress 事件中指定输入键盘码必须为数字: =48)&&(event.keyCode     注意:如果不允许输入小数点去掉“...

(转)解决Asp.net中文本框控件(TextBox)readonly或disabled时内容无法POST到服

网页中有的时候想让用户无法在文本框控件(TextBox)里输入,常见的方法是把它的ReadOnly属性设为True,或者是将Enabled属性设为False。这两种方法的作用差不多。另外还可以用标签代...

ASP.NET动态添加TextBox控件

ASP.NET动态添加TextBox控件

ASP.Net TextBox控件只允许输入数字(转)

TextBox控件只允许输入数字

asp.net学习之textbox控件使用的小例子

.apsx代码如下: 输入正确的姓名和密码(123456),解锁控件 姓名: ...

ASP.Net TextBox控件只允许输入数字

1.1、在Asp.Net TextBox 控件的 OnKeyPress 事件中指定输入键盘码必须为数字: OnKeyPress="if(((event.keyCode>=48)&&(event.ke...

ASP.Net TextBox控件只允许输入数字

转载:http://www.cnblogs.com/chenbg2001/archive/2009/05/22/1486968.html 1.1、在Asp.Net TextBox 控件的 O...

ASP.NET自定义Web服务器控件-TextBox文本框控件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)