Hi @TracyLiang ,
If we checked the checkbox in header of GridView, it works to trigger the event OnCheckedChanged; but if we clear it, there will be no trigger of it.
My understanding is that the checkbox works fine when checked, but not when unchecked.
I did a simple test based on the code you provided and didn't encounter a similar problem.
Maybe you can describe your problem more clearly and provide more information.
protected void CheckAll_Checked(object sender, EventArgs e)
{
CheckBox chkAll = (CheckBox)dgMain.HeaderRow.Cells[0].FindControl("chkAllofPage");
foreach (GridViewRow row in dgMain.Rows)
{
CheckBox chk = (CheckBox)row.Cells[0].FindControl("chkSelect");
chk.Checked = chkAll.Checked;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Data> lst = new List<Data>();
lst .Add ( new Data ( ) { Name = "A" });
lst .Add ( new Data ( ) { Name = "B" });
lst .Add ( new Data ( ) { Name = "C" });
dgMain.DataSource = lst;
dgMain.DataBind();
}
}
public class Data
{
public string Name { get; set; }
}
protected void dgMain_RowCreated(object sender, EventArgs e)
{
}
<asp:GridView runat="server" AllowSorting="True" AutoGenerateColumns="False" ID="dgMain" OnRowCreated="dgMain_RowCreated" EnableModelValidation="True">
<Columns>
<asp:TemplateField HeaderText="test" >
<HeaderTemplate>
<asp:CheckBox ID="chkAllofPage" runat="server" AutoPostBack="True" OnCheckedChanged="CheckAll_Checked" ToolTip="TEST" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<FooterStyle Width="20px" Wrap="False" />
<HeaderStyle Wrap="False" HorizontalAlign="Center" Width="20px"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center" Width="20px"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.