hi Robin,
I never expected to have to put all this time just to add that onclick
attribute to every checkbox in your checkboxlist control. Actually it does
sound very simple and then gets impossible --however I did get there
eventually.
At first I assumed trying to get to the items in the checkboxList item
collection and adding the onclick attribute would work, however no
attribute is being set --just turns up blank
For i = 0 To CheckBoxList1.Items.Count - 1
CheckBoxList1.Items.Item(i).Attributes.Item("onclick") =
"MyFunction();"
Next
whereas if you tried to set the text property of an item in the checkboxlist
it sets :S
Private Sub CheckBoxList1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBoxList1.Load
For i = 0 To CheckBoxList1.Items.Count - 1
CheckBoxList1.Items.Item(i).text = "New value"
Next
End sub
When trying to add the onclick attribute through the controls collection of
the CheckboxList control the individual checkboxes get wrapped around span
tags with the onclick attribute i add. This is the same behaviour you get
when trying to add attributes to a checkbox control.
Unfortunately this is the behaviour of the checkbox control which you cannot
change and is useless to you because this will return a span element and not
the checkbox in itself if you tried to access the checkbox from javascript
via that onclick function you attach, however you can easily work around
this if you tried to pass "this.firstChild" element in javascript to your
function, which will return your checkbox element.
like this :
Dim c As Control
For Each c In CheckBoxList1.Controls
If TypeOf c Is CheckBox Then
CType(c, CheckBox).Attributes.Add("Onclick",
"functionClick(this.firstChild);")
End If
Next
Control output :
<table id="CheckBoxList1" border="0" style="Z-INDEX: 102; LEFT: 284px;
POSITION: absolute; TOP: 294px">
<tr>
<td><span Onclick="functionClick(this.firstChild)"><input
id="CheckBoxList1_0" type="checkbox"
name="CheckBoxList1:0" /><label for="CheckBoxList1_0">1</label></span></td>
</tr><tr>
<td><span Onclick="functionClick(this.firstChild)"><input
id="CheckBoxList1_1" type="checkbox"
name="CheckBoxList1:1" /><label for="CheckBoxList1_1">2</label></span></td>
</tr>
</table>