ASP.NET Gridview与checkbox全选、全不选

1. 页面  在onclick事件中 "传自己"

 

代码
  < asp:TemplateField  HeaderText ="全选" >
      
< HeaderTemplate >
         
< input  type ="checkbox"  id ="CheckBox1"  name ="CheckBox1"  onclick ="GetAllCheckBox(this)"   />
             全选
      
</ HeaderTemplate >
      
< ItemTemplate >
           
< asp:CheckBox  ID ="CheckBox2"  runat ="server" />
      
</ ItemTemplate >
       
< ItemStyle  HorizontalAlign ="Center"  Width ="100px"   />
</ asp:TemplateField >

 

 2.编写JS代码

代码
     < script type = " text/javascript "  language = " javascript " >
        
function  GetAllCheckBox(CheckAll)
        {
            
var  items  =  document.getElementsByTagName( " input " );
            
for ( var  i = 0 ;i < items.length;i ++ )
            {
                
if (items[i].type  ==   " checkbox " )
                {
                    items[i].checked 
=  CheckAll.checked;
                }
            }
        }
    
< / script>

 

 

你可能感兴趣的:(checkbox)