在Gridview中加入下列模板:
<asp:TemplateField HeaderText="全选">
<HeaderTemplate>
<asp:CheckBox ID="checkall" runat="server" Text="全选"
AutoPostBack="true" OnCheckedChanged="checkAll" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="check" runat="server" Text="选择" />
</ItemTemplate>
</asp:TemplateField>
在CS文件中加入下列方法:
protected void checkAll(object sender, EventArgs e)
{
CheckBox cb = (CheckBox) sender;
if (cb.Text == "全选")
{
foreach (GridViewRow gvr in gvLessons.Rows)
{
CheckBox cb1 = (CheckBox)gvr.FindControl("check");
cb1.Checked = cb.Checked;
}
}
}