Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Select all checkbox in the data grid

5.00/5 (2 votes)
19 Apr 2011CPOL 8.5K  
Above aproach will work fine if there are less check box on the form. But if you have 1000 or 2000 check box on your page then it will consume some time.There is an alternate, unique and very fast way of doing the same thing.below code will create more than 2000 checkboxes on your...
Above aproach will work fine if there are less check box on the form. But if you have 1000 or 2000 check box on your page then it will consume some time.

There is an alternate, unique and very fast way of doing the same thing.
below code will create more than 2000 checkboxes on your page
<table width="150" id="ttt">
    <tr>
                <td>
                    <asp:checkbox id="chkSelectAll" runat="server" text="Select All" xmlns:asp="#unknown" />
                    <br />
                    <asp:checkbox id="chkBx1" runat="server" text="CheckBox 1" enabled="false" xmlns:asp="#unknown" />
                    <br />
                    <%for (int i = 0; i < 2000; i++)
                      { %>
                    <asp:checkbox id="chkBx2" runat="server" text="CheckBox 2" xmlns:asp="#unknown" />
                    <%} %>
                    <br />
                    <asp:checkbox id="chkBx3" runat="server" text="CheckBox 3" xmlns:asp="#unknown" />
                    <br />
                    <asp:checkbox id="chkBx4" runat="server" text="CheckBox 4" xmlns:asp="#unknown" />
                </td>
            </tr>
</table>

Now try to select all the check box using this approach.
As I think you can measure the difference.

<code>$(document).ready(
function(){
        var obj = $('#chkSelectAll');
        var obj1 = $('input:checkbox:not(:disabled)').not('#chkSelectAll');
        var total_count = obj1.length;
        
        obj.click(function()
                  {
                        var chk_Count = 0;
                        if(!this.checked)
                            chk_Count = $('input:checkbox:not(:disabled):checked').not('#chkSelectAll').length
                        else    
                            chk_Count = obj1.length;
                            
                           obj1.click(function()
                                      {
                                            if(!this.checked)
                                            {
                                                obj.attr('checked', false)
                                                chk_Count = chk_Count -1;
                                                return;
                                            }
                                            else
                                            {
                                                chk_Count = chk_Count + 1;
                                                
                                                if(chk_Count == total_count)
                                                    obj.attr('checked',true)
                                                else    
                                                    obj.attr('checked', false)
                                            }
                                       }
                                    ).attr('checked', this.checked);
                        }
                    )
                }
);
</code>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)