Hi,
john wrote:
I am using ASP.NET and I have a datagrid. One of the columns in my
grid is all checkboxes. When the user clicks on a certain button on
the page, which is not in the grid, I want to be able to traverse
through all the checkboxes in that column and see how many are
checked. This is so that I can give them a confirmation dialog before
I do an action on the selected rows. How can I use javascript to
traverse through the checkboxes and count how many are checked?
Thanks in advance
An ASP.NET DataGrid is nothing else (once the HTML code has been
produced) than a HTML table. The form elements appearing into the
grid/table all belong to a form.
If you want to check all the checkboxes on the form, the easiest is to
loop through the form, and check the type of each element. Something like:
<script type="text/javascript">
<!--
function m_bnButtonName_OnClick( frm )
{
var iChecked = 0;
for ( var index = 0; index < frm.elements.length; index++ )
{
var oElement = frm.elements[ index ];
if ( oElement.type == "checkbox" )
{
if ( oElement.checked )
{
iChecked++;
}
}
}
alert( iChecked + " checkboxes checked" );
}
//-->
</script>
<form name="frmTest">
<input type="checkbox" id="cb1">CB1
<br />
<input type="checkbox" id="cb2">CB2
<br />
<input type="checkbox" id="cb3">CB3
<br />
<input type="checkbox" id="cb4">CB4
<br />
<input type="checkbox" id="cb5">CB5
<br />
<input type="checkbox" id="cb6">CB6
<br />
<input type="button" name="m_bnButtonName"
value="Count" onclick="m_bnButtonName_OnClick(this.form);">
</form>
Just be careful that your button must be a client-side button (from the
HTML toolbox), *not* a server-side button (from the ASP toolbox)!
Hope that helps,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering:
http://www.galasoft-LB.ch
Private/Malaysia:
http://mypage.bluewin.ch/lbugnion
Support children in Calcutta:
http://www.calcutta-espoir.ch