脚本实现全选GridView中的CheckBox

今天终于实现了全选CheckBox而不用AJAX或者回传事件,省时又效率

    全选和全不选,不会出现选中的项目全选时却不选的情况

前台代码如下:

GridView代码:

 

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="PermitID" >
          <Columns>
           <asp:TemplateField>
               <HeaderTemplate>
                   <input  id="CheckAll" type="checkbox" onclick="selectAll(this);" />
               </HeaderTemplate>
               <ItemTemplate>
                   <asp:CheckBox runat="server" ID="checkbox"  />
               </ItemTemplate>
               <ItemStyle Width="100px" />
           </asp:TemplateField>
           <asp:BoundField DataField="PermitCode" HeaderText="权限代码" SortExpression="PermitCode" />
           <asp:BoundField DataField="PermitName" HeaderText="权限名称" SortExpression="PermitName" />
          </Columns>
          <HeaderStyle CssClass="GVHeader" />
          <AlternatingRowStyle CssClass="GVAlternatingRow" />
          <RowStyle CssClass="GVRow" />
          <PagerStyle CssClass="GVPage" />
          <PagerSettings Visible="False" />
</asp:GridView>

 

其发挥作用的脚本如下:

 <script type="text/javascript">    
    function selectAll(obj)
    {
        var theTable  = obj.parentElement.parentElement.parentElement;
        var i;
        var j = obj.parentElement.cellIndex;
       
        for(i=0;i<theTable.rows.length;i++)
        {
            var objCheckBox = theTable.rows[i].cells[j].firstChild;
            if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
        }
    }
</script>

你可能感兴趣的:(JavaScript,Ajax,脚本,J#,asp)