Uğur UMUTLUOĞLU
Web Technologies, Microsoft and .NET Technologies

CheckBoxList: Select All / Clear All

Wednesday, 24 June 2009 23:17 by ugur

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>&nbsp;

<a href="javascript:ListeSecim('CheckBoxList1', false)">Clear All</a>

 

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed
 
Add to Technorati Favorites