我在尝试选中标题复选框时选中所有复选框。
我为此编写了正确的代码,但不知道它为什么不起作用。
任何人都可以检查问题。
我的代码 -
<head runat="server">
<title></title>
<script type="text/javascript">
function SelectAllCheckboxes1(chk) {
$('#<%=GridView1.ClientID%>').find("input:checkbox").each(function () {
if (this != chk) { this.checked = chk.checked; }
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" AutoPostBack="true"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:gridview>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HRMSConnectionString %>"
SelectCommand="SELECT [Name] FROM [Languages]"></asp:SqlDataSource>
</div>
</form>
</body>
答案 0 :(得分:2)
试试这段代码 -
<强> default.aspx.cs 强>
protected void sellectAll(object sender, EventArgs e)
{
CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkb1");
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkb2");
if (ChkBoxHeader.Checked == true)
{
ChkBoxRows.Checked = true;
}
else
{
ChkBoxRows.Checked = false;
}
}
}
更改网格代码 -
<asp:gridview ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkb1" runat="server" AutoPostBack="true" OnCheckedChanged="sellectAll"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkb2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:gridview>