checkboxlist count

S

suzy

i have generated a list of checkboxes (at runtime) using the checkboxlist
control.


i want to add an onclick event onto each checkbox so i can perform a count
of how many items have been selected every time a checkbox is clicked.

how can i do this? remember, the checkboxes are generated at runtime.

thanks
 
R

Rick Spiewak

You should be able to add an eventhandler to each checkbox as you create
it - which you'll need to do on postback as well. Since all you want to do
is count, you can use the same handler.
 
S

suzy

but what if the checkboxlist is created using a databind method, rather than
a loop?

how do i assign an eventhandler to each checkbox then?
 
R

Rick Spiewak

You will need to use .Attributes.Add to attach javascript to each
checkbox as you add the checkbox, and then keep track in a javascript
var which you will use to present the information via the .innerHTML
property of a span, for instance. The javascript will need to be
inserted after the <form>, so that it will be able to recognize the
<span> tag, etc..
 
S

suzy

rick,

like i said i am not adding the checkboxes individually. i am assigned a
DataSource to the checkboxlist control (which contains a datatable of the
values of the checkboxes). then i am calling the .databind method of the
checkboxlist control.

if i call .attributes of the checkboxlist control, the attribute is added at
the table level, not the checkbox level.

do u have an example?
 
R

Rick Spiewak

Then you need to do this after the databind, possibly in the page
pre-render event. Walk the controls collection of the checkboxlist, and
add the attribute to each listitem type you find.
 
S

suzy

foreach (ListItem item in ((CheckBoxList)
chkList.FindControl("chkList")).Items)

{

item.Attributes.Add ("onclick",
string.Format("javascript:check_Click({0});", item.Value));

}



rick, i have typed the following code after the databind call. the code
runs without errors (and the lines of code are being called when i step
through the code), but there is no sign of any onclick attriibute on the
client (even if i view source of the html page). it is nowhere. what am i
doing wrong?
 
R

Rick Spiewak

I have to admit, I'm stumped at the moment. I tried what I suggested to
you, it doesn't work for me, either. Then, I tried a pure javascript
approach:
..
..
</form>
<script language="javascript">
var chkboxList = document.all.CheckBoxList1;
var chkboxes = chkboxList.getElementsByTagName("input");
for( i = 0; i < chkboxes.length; ++i ) {
var chkbox = chkboxes(i);
var action = document.createAttribute("onclick");
action.value = "foo()";
chkbox.attributes.setNamedItem(action);
}
..
..
</body>

This *seems* to work in debug mode, but once again there is no attribute
output.

So, then I tried setting autopostback = true for the control. As long as
I took out all the other attempts at setting attributes, this works -
but it's on the server side.

However, that's the only technique I was able to use which allows
collecting these events!

Anyone else out there have a better idea? Microsoft?
 
S

suzy

Hi Rick,

Thanks for your help, it seems like a bug in .NET (see previous post). In
the end I got it working but guess what?! I did it the old fashioned
classic ASP way (ie: manually build the html). There probably is a better
way (is there?), but it's a quick fix, and the main thing is it's working!

I wasted waaaaaaay too much time on this! :)

Thanks again!
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top