客户端实现GridView全选全不选

Code:
  1. <script type="text/javascript">   
  2.     function change(sender)   
  3.     {   
  4.         var checks = document.getElementById('<%=GridView1.ClientID %>');   
  5.         if(sender.checked)   
  6.         {   
  7.             alert(checks.rows.length.toString());   
  8.             for(var i=1;i<checks.rows.length;i++)   
  9.             {   
  10.                 var ch = checks.rows[i].cells[0].getElementsByTagName("input")[0];   
  11.                ch.checked = true;   
  12.             }   
  13.         }   
  14.         else  
  15.         {   
  16.            for(var  i=1;i<checks.rows.length;i++)   
  17.             {   
  18.                 checks.rows[i].cells[0].getElementsByTagName("input")[0].checked = false;   
  19.             }   
  20.         }   
  21.     }   
  22.     </script>   

 

GridView:

Code:
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"    
  2.             DataKeyNames="sid" DataSourceID="SqlDataSource1">  
  3.             <Columns>  
  4.                 <asp:TemplateField>  
  5.                     <HeaderTemplate>  
  6.                        <input ID="Checkbox1" runat="server" onclick="change(this);" type="checkbox" /></HeaderTemplate>  
  7.                     <ItemTemplate>  
  8.                         <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True"    
  9.                              />  
  10.                     </ItemTemplate>  
  11.                 </asp:TemplateField>  
  12.                 </Columns>  
  13.         </asp:GridView>  

 

你可能感兴趣的:(客户端实现GridView全选全不选)