How can I traverse through all of the checkboxes in my datagrid using javascript?

  • Thread starter Thread starter john
  • Start date Start date
J

john

I posted this question to comp.lang.javascript but didn't get a
response, so I'll try here.

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
 
john said:
I posted this question to comp.lang.javascript but didn't get a
response, so I'll try here.

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

Hi John,


<asp:CheckBoxList id=CheckBoxList1 runat="server">
<asp:ListItem Value="1">Item1</asp:ListItem>
<asp:ListItem Value="2">Item2</asp:ListItem>
<asp:ListItem Value="3">Item3</asp:ListItem>
<asp:ListItem Value="4">Item4</asp:ListItem>
</asp:CheckBoxList>

<script language=javascript>
// Go through all items of a check list control
var table = document.getElementById ('CheckBoxList1');
var cells = table.getElementsByTagName("td");
var ctlr;

for (var i = 0; i < cells.length; i++)
{
ctrl = cells.firstChild;

if (ctrl.type == 'checkbox')
alert ('Name:' + ctrl.name + '; selected: ' + ctrl.checked);
}
</script>

Note that this is just a CheckBoxList. I have an <asp:Panel> control which
rendered as a table which is why I did traversal through <td>s. A datagrid
is a table as well. I don't know how many columns your datagrid has and
which column contains checkboxes, but the idea should be the same---through
DOM calls you can navigate controls in your datagrid and find checkboxes
this way.

If you want to traverse checkboxes in your datagrid server-side it's
somewhat different.
 
Back
Top