Gridview Header Checkbox Select and Deselect ALL Rows Checkbox


Import Jquery

<script src="jquery.min.js"></script>


Use this Code With in Head Tag:

<script type="text/javascript">
        $(document).ready(function () {
            $('#chkselectall').click(function () {
                if ($('#chkselectall').is(':checked')) {
                    $('[id$=testGrd]').find('input:checkbox').prop('checked', true);
                }
                else {
                    $('input:checkbox[name$=chkid]').each(
                        function () {
                            $(this).removeAttr('checked');
                        });
                }
            });
        });
       
    </script>



GridView:

<table id="ContentPlaceHolder1_testGrd" cellspacing="0" border="1">
                              <tr>
                                <td><b>Sl#</b></td>
                                <td><b> Name</b></td>
                                <td><b> Image</b></td>
                                <td>Select All<input type="checkbox" id="chkselectall" title="Select All" /></td>
                              </tr>
  </table>
                             
    <asp:GridView ID="testGrd" runat="server" AutoGenerateColumns="False" ShowHeader="false">
          <Columns>
                <asp:BoundField SortExpression="Sl"  DataField="Sl" HeaderText="Sl#" />
                                          
                   <asp:TemplateField HeaderText="Name" SortExpression="Name">
                     <ItemTemplate>
                        <asp:Label runat="server" ID="lblName" Text='<%# Bind("Name") %>'>                           </asp:Label>              
                      </ItemTemplate>
                    </asp:TemplateField>

                  <asp:TemplateField HeaderText="Image" SortExpression="Image">
                    <ItemTemplate>
                  <asp:Image ID="Image1" runat="server" ImageUrl ='<%# "~/Uploads/Image/"+  Eval("Image") %>' />
                  </ItemTemplate>
                 </asp:TemplateField>

                <asp:TemplateField HeaderText="Select" SortExpression="Select">
                    <ItemTemplate>
                       <asp:CheckBox ID="chkid" value='<%# Eval("Id") %>' runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

     </asp:GridView>

Comments

Popular posts from this blog

Unable to perform operation on the item is locked in workspace

Maximum Stored Procedure Function Trigger or View Nesting Level Exceeded (limit 32) in SQL Server

Insecure cookie setting: missing Secure flag