Coding: Use Javascript to select/unselect all (input box/check box)

3 08 2007

Reference: (modified slightly to fit project)

Javascript:

<script language=”javascript”>
function check_uncheck(Val)
{
var ValChecked = Val.checked;
var ValId = Val.id;
var frm = document.forms[0];
// Loop through all elements
for (i = 0; i < frm.length; i++)
{
// Look for Header Template’s Checkbox
//As we have not other control other than checkbox we just check following statement
if (this != null)
{
if (ValId.indexOf(‘CheckAll’) !=  – 1)
{
// Check if main checkbox is checked,
// then select or deselect datagrid checkboxes
if (ValChecked)
frm.elements[i].checked = true;
else
frm.elements[i].checked = false;
}
else if (ValId.indexOf(‘deleteRec’) !=  – 1)
{
// Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
if (frm.elements[i].checked == false)
frm.elements[1].checked = false;
}
} // if
} // for
} // function
</script>

VB Code:
If using the input box
<input type = ‘checkbox’ ID=’CheckAll’ onclick=’return check_uncheck (this );’ runat=’server’ />

If using the check box
<asp:CheckBox ID=”CheckAll” onclick=”return check_uncheck (this );” runat=”server” />


Actions

Information

Leave a comment