By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

checkboxlist count

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.

"suzy" <su**@spam.com> wrote in message
news:KG******************@doctor.cableinet.net...
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

Nov 18 '05 #2

P: n/a
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?
"Rick Spiewak" <ri*********@mindspring.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

"suzy" <su**@spam.com> wrote in message
news:KG******************@doctor.cableinet.net...
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


Nov 18 '05 #3

P: n/a
ps: i want the count to be performed on the client-side
"Rick Spiewak" <ri*********@mindspring.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

"suzy" <su**@spam.com> wrote in message
news:KG******************@doctor.cableinet.net...
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


Nov 18 '05 #4

P: n/a
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..

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #5

P: n/a
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?
"Rick Spiewak" <ri*********@mindspring.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
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..

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #6

P: n/a
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #7

P: n/a
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?

"Rick Spiewak" <ri*********@mindspring.com> wrote in message
news:eB*************@TK2MSFTNGP11.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #8

P: n/a
http://support.microsoft.com/default...;en-us;Q309338
"suzy" <su**@spam.com> wrote in message
news:XS******************@pathologist.blueyonder.n et...
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?

"Rick Spiewak" <ri*********@mindspring.com> wrote in message
news:eB*************@TK2MSFTNGP11.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 18 '05 #9

P: n/a
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #10

P: n/a
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!
"Rick Spiewak" <ri*********@mindspring.com> wrote in message
news:u5*************@TK2MSFTNGP10.phx.gbl...
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.