GridView checkBox列全选

GridView checkBox列全选

<form id="form1" runat="server">
<div>
<asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
     <HeaderTemplate>Select All
           <input type="checkbox" id="chkAll" name="chkAll" onclick="Check(this,'^gvCategories')"/>
       </HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
 
<asp:TemplateField HeaderText="Category Name">
<ItemTemplate>
<asp:Literal ID="litCategoryName" runat="server" Text = '<%# Eval("CategoryName") %>' />
</ItemTemplate>
 
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Select All
<input type="checkbox" id="chkAll" name="chkAll" onclick="Check(this,'^gvProducts')"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:Literal ID="litProductName" runat="server" Text = '<%# Eval("ProductName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
 
<script language="javascript" type="text/javascript">
//var pattern = '^gvCategories';
function Check(parentChk, pattern)
{
var elements = document.getElementsByTagName("INPUT");
for(i=0; i<elements.length;i++)
{
  if(parentChk.checked == true)
{
if( IsCheckBox(elements[i]) && IsMatch(elements[i].id, pattern))
{
elements[i].checked = true;
}
}
else
{
if( IsCheckBox(elements[i]) && IsMatch(elements[i].id, pattern))
{
elements[i].checked = false;
}
}
}
}
 
function IsMatch(id, pattern)
{
var regularExpression = new RegExp(pattern);
return id.match(regularExpression);
}
function IsCheckBox(chk)
{
return (chk.type == 'checkbox');
}
</script>
</form>
</body>
</html>

你可能感兴趣的:(GridView checkBox列全选)