CheckBoxList 和 RadioButtonList 提供了复选框和单选框的列表方式的控制,非常简单实用。它们的 Items 集合成员为 ListItem 对象,输出到页面上为 Input 元素,类型分别为 CheckBox 和 RadioBox 。但 ListItem 对象并没有 ClientID 属性,也就无法获得其生成的网页元素 Input 的ID,那么如何编写所生成的 Input 元素的客户端事件呢?
通过查看输出页面的源文件可以获知,其生成的 Input 元素的 ID 为:CheckBoxList 或 RadioButtonList 控件的 ClientID 属性值 + 下划线 + ItemIndex
知道了这个规律,这样就可以对 ListItem 所生成的 Input 进行控制了。如在 aspx 页面中写:
<script type="text/javascript">
window.οnlοad=function(){
alert(<%=CheckBoxList1.ClientID%>_0.checked);
}
</script>
定义事件也很简单,如定义onclick事件:
<script type="text/javascript">
window.οnlοad=function(){
<%=CheckBoxList1.ClientID%>_0.οnclick=function(){
alert('单击事件被激发');
}
}
</script>