1. 页面 在onclick事件中 "传自己"
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<
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 >
< 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代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<
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>
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>