473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

checkboxlist count

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
10 1936
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robin Day | last post by:
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...
3
by: I am Sam | last post by:
I keep getting the following error message when I try to iterate through a CheckBoxList control: Object reference not set to an instance of an object. Description: An unhandled exception...
3
by: Jack Black | last post by:
Using VS.Net 2k3 to build ASP.Net app with VB code-behind pages... Hi, all! I've been struggling with getting a dynamically-generated CheckBoxList generated. I've finally been able to get the...
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
4
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to...
0
by: Patrick Olurotimi Ige | last post by:
I have a CheckBoxList(which is Databinded) in a Datalist. My DataBinded is done in the onItemDataBound I'm using this method below to select the selected item in a DataBinded CheckBoxList. ...
2
by: Patrick.O.Ige | last post by:
I have some boolean value(1 or 0 ) in a table and i want a databinded CheckBoxList to present the selected values on the page.. With CheckBox i know i can se the Checked property like so :-...
0
by: Michael Manuel via .NET 247 | last post by:
I seem to be having a lot of trouble using the CheckBoxList Control. I have created two pages to try and find out what I have been doing wrong. Both pages are causing me problems but different...
3
by: Stephen | last post by:
Hi, I have a question on checkboxlist, suppose I have a checkboxlist and has listitems like USA Canada UK Germany Australia Suppose I have a resultset that has values (USA, UK and Australia)
4
by: haresh.amis | last post by:
hello to all, I m using .net 2.0 and i face a problem that is as under Well I have a checkboxlist which i bound in .cs page now I want to count that how many checkboxes ate checked ( In...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.