How can I traverse through all of the checkboxes in my ASP.NET datagrid?

J

john

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
 
K

kaeli

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

Datagrid is an ASP term. I have no clue what that is.
What does the html look like?

Theorectically, if you name all the checkboxes in the group the same
name, javascript will consider it an array and you can loop it.
This might make your server-side code a PITA though.

--
 
L

Laurent Bugnion

Hi,
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
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top