Checkboxlist Clientside rather than Serverside

R

Robin Day

I run some code on the changed event of checkbox lists. Its quite simple,
nothing more than showing / hiding some other parts of the page.
Using Autopostback and Server side code works fine, but is far too slow,
especially if the users are on modems.
I need to quite simply, add some java code to the onclick event of the input
tags of a checkboxlist.
However... Attributes.Add adds the onclick attribute and code to the
outlying table that the framework creates.
Is there a way of getting this code and events into the input tags?

Any help or ideas much appreciated.

Thanks
Robin
 
S

Scott M.

Have you tried using individual checkbox controls with the attributes.add
rather than a checkboxlist control?
 
R

Robin Day

The way the checkboxlist control works is very useful in this application.
It is deeply embedded into a number of custom webcontrols that are used in
various places on a number of systems.
I really don't want to have to produce my own version of the checkboxlist
when the only functional change I require is to add some onclick javascript.
Is it possible that there's some other way of accesseing the individual
input tags as the control renders? So I can manually add the events there?
Or possibly a way of using a custom validator that could fire the javascript
instead?

Thanks,
Robin
 
A

Alessandro Zifiglio

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top