asp:CheckBox event trigger issue

TracyLiang 41 Reputation points
2022-07-31T01:36:47.94+00:00

Dear all,

Recently we found a problem of checkbox in GridView: 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.
Please kindly support to find the rootcause of it. Many thanks!
<asp:GridView runat="server" AllowSorting="True" AutoGenerateColumns="False" ID="dgMain" OnRowCreated="dgMain_RowCreated" EnableModelValidation="True">
<Columns>
<asp:TemplateField HeaderText="<%$ Resources:GlobalText, SelectedField_HeaderText %>" >
<HeaderTemplate>
<asp:CheckBox ID="chkAllofPage" runat="server" AutoPostBack="True" OnCheckedChanged="CheckAll_Checked" ToolTip="<%$ Resources:GlobalText, CheckAll_ToolTip %>" />
</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>
...
</asp:GridView>

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,290 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 25,956 Reputation points Microsoft Vendor
    2022-08-01T05:58:48.567+00:00

    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>  
    

    226539-test.gif
    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.


  2. AgaveJoe 26,146 Reputation points
    2022-08-01T17:36:24.8+00:00

    I modified LanHuang-MSFT's code slightly and added the GridView to an acsx and the OnCheckedChanged handler fires on every change. Keep in mind, you can do the same.

    As recommended, share your code if you want community debugging/code review support.

    0 comments No comments