65.9K
CodeProject is changing. Read more.
Home

Select all checkbox in the data grid

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Apr 19, 2011

CPOL
viewsIcon

8482

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
<asp:checkbox id="chkSelectAll" runat="server" text="Select All" xmlns:asp="#unknown" />
<asp:checkbox id="chkBx1" runat="server" text="CheckBox 1" enabled="false" xmlns:asp="#unknown" />
<%for (int i = 0; i < 2000; i++) { %> <asp:checkbox id="chkBx2" runat="server" text="CheckBox 2" xmlns:asp="#unknown" /> <%} %>
<asp:checkbox id="chkBx3" runat="server" text="CheckBox 3" xmlns:asp="#unknown" />
<asp:checkbox id="chkBx4" runat="server" text="CheckBox 4" xmlns:asp="#unknown" />
Now try to select all the check box using this approach. As I think you can measure the difference.
$(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);
                        }
                    )
                }
);