简体   繁体   English

如果未选中gridview行中的所有复选框,请取消选择复选框标题

[英]Unselect checkbox header if not all of checkbox in the gridview rows is checked

as the question says, I already can check the checkboxes in the gridview rows when the "Select All" CheckBox in the header is checked and uncheck the checkboxes in the gridview rows when the "Select All" CheckBox in the header is unchecked. 如问题所述,当标题中的“全选”复选框被选中时,我已经可以在gridview行中选中复选框,而当标题中的“全选”复选框未选中时,则可以取消选中Gridview行中的复选框。 And I want to do when not all of the checkboxes in the rows is checked, then the "Select All" CheckBox in the header is not checked, also vice versa (when all of the checkboxes in the rows is checked, then the "Select All" CheckBox in the header is checked). 我想做的是,当未选中行中的所有复选框时,未选中标题中的“全选”复选框,反之亦然(当选中行中的所有复选框时,则“选择”标头中的“所有”复选框将被选中)。

How can I do that? 我怎样才能做到这一点?

I already did like I want to achieve, but the checkbox in the header starts to affect (checked or unchecked) even I only check or uncheck a single checkbox in the rows, not all of them. 我已经确实想要实现,但是标题中的复选框开始影响(选中或取消选中),即使我只选中或取消选中行中的单个复选框,也并非全部选中。

Here is the code that I am using: 这是我正在使用的代码:

protected void checkAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");

        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkbx_select");

            if (ChkBoxHeader.Checked)
            {
                ChkBoxRows.Checked = true;
            }

            else
            {
                ChkBoxRows.Checked = false;
            }
        }
    }

protected void chkbx_select_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkbx_select =  (CheckBox)sender;

        CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");

        foreach (GridViewRow row in GridView1.Rows)
        {
            if (chkbx_select.Checked)
            {
                ChkBoxHeader.Checked = true;
            }

            else
            {
                ChkBoxHeader.Checked = false;
            }
        }
    }

Your answer much appreciated. 非常感谢您的回答。

Thank you. 谢谢。

The idea is on every CheckedChanged event to iterate over all checkboxes (in all rows) and make sure all of them are checked. 这个想法是在每个CheckedChanged事件上迭代所有复选框(在所有行中)并确保选中所有复选框。 Something like this 像这样

protected void chkbx_select_CheckedChanged(object sender, EventArgs e)
{    
        CheckBox chkbx_select =  (CheckBox)sender;
        CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("checkAll");

        if(!chkbx_select.Checked)
        {
           // Checkbox was unchecked, 
           // short circuit, set Select All Checkbox.Checked = false and return

           ChkBoxHeader.Checked = false;
           return;
        }

        bool allChecked = true;
        foreach (GridViewRow row in GridView1.Rows)
        {
            // this is pseudocode, find checkbox on each row
            var checkBox = row.FindControl("Checkbox") 

            if (!checkbox.Checked)
            {
                allChecked = false;
                break;
            }
        }

        ChkBoxHeader.Checked = allChecked;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM