在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>
生成的客戶端代碼如下:
<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>
標簽:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:CSDN