GridView 增加一列CheckBox 并且可以全选

这个功能很常用.GridView 中需要有一列CheckBox 可以单独选择.也可以通过列头中的CheckBox 全部选择.

JS:

 

<script language="javascript" type="text/javascript">
       function SelectAll(tempControl)
       {
           //将除头模板中的其它所有的CheckBox取反

            var theBox=tempControl;
             xState=theBox.checked;   

            elem=theBox.form.elements;
            for(i=0;i<elem.length;i++)
            if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
             {
                  if(elem[i].checked!=xState)
                        elem[i].click();
            }
  } 
</script>

 

GridView column:

                        <asp:TemplateField>
                                <HeaderTemplate>
                                       <asp:CheckBox id="chkHeader" runat="server" AutoPostBack="False"  
                                                onclick="javascript:SelectAll(this);"></asp:CheckBox>                            
            </HeaderTemplate>

                           <ItemTemplate>
                                       <asp:CheckBox id="chkItem" runat="server"></asp:CheckBox>
                           </ItemTemplate>
                        </asp:TemplateField> 

 

posted on 2009-10-22 10:18  AndyCai  阅读(524)  评论(0编辑  收藏  举报

导航