Actually it is not related to ASP.NET directly but this code snippet could be useful ASP.NET programmers anyway. Scenario is something like that; we have many record listed in CheckBoxList and we want all of the records can be selected or selected items can be deleted by one move. We can handle this easily by using a simple JavaScript function that is seen below.
<script language="javascript" type="text/javascript">
function ListeSecim(kontrol, durum) {
var ctl = document.getElementById(kontrol);
var inputs = ctl.getElementsByTagName('input'); //Bring input elements in CheckBoxList
//Assign checked property of all input elements in CheckBoxList to the value in state variable (true/false)
for (var i = 0; i < inputs.length; i++)
inputs[i].checked = durum;
}
</script>
Of course, additionally we need links that will do selecting and deleting all items. We will call the function above by using these links. The only difference between these two links is the value of second parameter that will sent to ListSelect function.
<a href="javascript:ListeSecim('CheckBoxList1', true)">Select All</a>
<a href="javascript:ListeSecim('CheckBoxList1', false)">Clear All</a>