在ASP.NET 2.0及其以后的版本中, CheckBox 控件新增加了两个属性:InputAttributes 和 LabelAttributes 。利用这两个属性,可以很方便地为label和input标签添加自定义属性,而使用 Attributes 则是不能完成这个任务的。不过,这个功能有些人还不知道,常被忽略。下面就是他们的使用方法:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script runat ="server" >
protected void Page_Load(object sender, EventArgs e)
{
CheckBox1.InputAttributes.Add( " onmouseover " , " alert('I am input/'s mouseover 事件哦。') " );
CheckBox1.LabelAttributes.Add( " onmouseover " , " alert('I am label/'s mouseover 事件哦。') " );
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 孟宪会之测试 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:CheckBox ID ="CheckBox1" runat ="server" Text ="选择项" />
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script runat ="server" >
protected void Page_Load(object sender, EventArgs e)
{
CheckBox1.InputAttributes.Add( " onmouseover " , " alert('I am input/'s mouseover 事件哦。') " );
CheckBox1.LabelAttributes.Add( " onmouseover " , " alert('I am label/'s mouseover 事件哦。') " );
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 孟宪会之测试 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:CheckBox ID ="CheckBox1" runat ="server" Text ="选择项" />
</ form >
</ body >
</ html >
生成的客户端代码如下:
<
input
id
="CheckBox1"
type
="checkbox"
name
="CheckBox1"
onmouseover
="alert('I am input's mouseover 事件哦。')"
/><
label
for
="CheckBox1"
onmouseover
="alert('I am label's mouseover 事件哦。')"
>
选择项
</
label
>