Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Select all checkbox in the data grid

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Apr 2011CPOL 8.1K   2   1
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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThese solutions will work in all the browser because JQuery ... Pin
nit_singh20-Apr-11 0:40
nit_singh20-Apr-11 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.