vendredi 25 septembre 2015

Checkbox in header will check or uncheck the checkbox in the rows, but only detect the last checkbox in the rows

I am trying to do the check box in the header will respond to any of checked or unchecked checkbox in the grid view rows. But, the check box header only respond when I check or uncheck the last checkbox in the gridview (The check box header will be checked if all of the checkbox in the gridview is checked and vice versa).

How can I solve not only last checkbox header which will respond to the last checkbox in the gridview, but every checkbox in the gridview?

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.Enabled)
            {
                ChkBoxRows.Checked = true;
            }

            else if (!ChkBoxHeader.Checked)
            {
                ChkBoxRows.Checked = false;
            }
        }
    }

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

        bool isAllChecked = false;

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

            if (!checkBox.Checked && checkBox.Enabled)
            {
                isAllChecked = false;
            }

            else if (checkBox.Checked)
            {
                isAllChecked = true;
            }
        }

        ChkBoxHeader.Checked = isAllChecked;
    }

Your answer much appreciated.

Thank you.




Aucun commentaire:

Enregistrer un commentaire