GridView 全选
GridView全选处理:
1.客户端脚本实现。
2.服务器端实现。
View Code
<script type="text/javascript"> function ConfirmSubmit() { if (confirm('您确定要删除吗?')) { return true; } else { return false; } } function Check(parentChk,ChildId) { var oElements = document.getElementsByTagName("INPUT"); var bIsChecked = parentChk.checked; for (i=0; i<oElements.length;i++) { if (IsCheckBox(oElements[i]) && IsMatch(oElements[i].id, ChildId)) { oElements[i].checked = bIsChecked; } } } function IsMatch(id, ChildId) { //gvList_ctl06_CheckBox1 var sPattern = '^gvList_ct.*' + ChildId + '$'; var oRegExp = new RegExp(sPattern); if(oRegExp.exec(id)) return true; else return false; } function IsCheckBox(chk) { if(chk.type == 'checkbox') return true; else return false; } </script>
View Code
<asp:GridView ID="gvList" runat="server" BorderStyle="Solid" CellPadding="3" PageSize="15" AutoGenerateColumns="False" Width="100%" CssClass="GridCss" OnRowDataBound="gvList_RowDataBound" OnRowCommand="gvList_RowCommand" > <Columns> <asp:TemplateField HeaderText="选择"> <HeaderTemplate> <%--<input type="checkbox" id="checkboxAll" name="checkboxAll" onclick="Check(this,'CheckBox1')" />全选--%> <asp:CheckBox ID="CheckBox2" runat="server" Text="全选" oncheckedchanged="CheckBox3_CheckedChanged" AutoPostBack=true /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" Width="40px" /> </asp:TemplateField> </asp:GridView>
View Code
protected void CheckBox3_CheckedChanged(object sender, EventArgs e) { CheckBox CheckBox3 = (CheckBox)sender; for (int i = 0; i < gvList.Rows.Count; i++) { CheckBox cbox = (CheckBox)gvList.Rows[i].FindControl("CheckBox1"); cbox.Checked = CheckBox3.Checked; } }