| <table border="0" style="font-size:9pt" onmouseover="mouseover(this,''''#ff0044'''')" onmouseout="mouseout(this)"> <tr > <td><span>姓名:</span></td><td><input name="_ctl2" type="text" /></td> </tr> </table> </BODY> </HTML> 新建一个文本文件,将这段代码复制过去,然后将文件的扩展名改为.html,用IE打开,是不是看到了想要的效果? 2、 前期工作准备完成了,接下来打开VS.NET IDE,新建一个项目,选择Visual C#项目(别问我要VB.net的代码,我不会),在右边列表中选择“WEB控件库”,输入项目名称:LabelTextBox。 3、 把自动生成的代码删除,只留下一个类的框架,像下面这样(命名空间自定): using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Drawing; namespace LzhTextBpx { public class LabelTextbox : System.Web.UI.WebControls.WebControl { } } 4、 上面看到的效果其实是一个一行两列的表格,我们把表格的border属性设成0,所以看不出来了,其中左边单元格放标签,右边单元格放文本框,所以,我们要用代码生成这个表格。用到的类有: a) Table:表示表格 b) TableRow:表示表格行 c) TableCell:表示表格中的单元格 注意:一个表格可以有多个表格行,一个表格行中可以放置多个单元格 生成代码如下: //定义一个表对象 Table t = new Table(); //添加一行 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); 我们还要响应表格的鼠标移入移出事件,背景色的变化就是在这里触发的,下面的代码完成此事: //添加鼠标事件 t.Attributes.Add("onmouseover","mouseover(this,''''" + "#" + R + G + B + "'''')"); t.Attributes.Add("onmouseout","mouseout(this)"); Attributes表示表格的属性集,Add()方法用于添加一个新的属性。 顺便把表格中的字体也设置一下: //添加样式,用来控制字体 t.Style.Add("font-size","10pt"); 看到上面的R、G、B三个变量了吗?这三个变量是某种颜色的十六进制的字符串表示。使用如下的方法对颜色进行分解: //以下将颜色值转化成十六进制表示 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"); 其中_backgroundColor是自定义属性。 上一页 [1] [2] [3] [4] [5] 下一页 |