标题中的复选框将选中或取消选中行中的复选框,但仅检测行中的最后一个复选框

时间:2015-09-25 07:23:24

标签: c# asp.net gridview checkbox

我正在尝试执行标题中的复选框将响应网格视图行中的任何已选中或未选中复选框。但是,复选框标题仅在我选中或取消选中gridview中的最后一个复选框时才会响应(如果检查了gridview中的所有复选框,则将检查复选框标题,反之亦然。)

我怎样才能解决最后一个复选框标题,它会响应gridview中的最后一个复选框,而不是网格视图中的每个复选框?

以下是我正在使用的代码:

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;
    }

你的回答非常感谢。

谢谢。

2 个答案:

答案 0 :(得分:0)

试试这段代码。

如果选中标题复选框。

    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 (ChkBoxRows!=null)
                    {
                        if(ChkBoxHeader.Checked)
                           ChkBoxRows.Checked = true;
                        else
                           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 ChkBoxRows = (CheckBox)row.FindControl("chkbx_select");

                if (!(ChkBoxRows.Checked && ChkBoxRows.Enabled))
                {
                    isAllChecked = true;
                }
            }

            if (!isAllChecked)
            {
                ChkBoxHeader.Checked = true;
            }
}

让我知道它是否有效。

答案 1 :(得分:0)

用于检查个人 Check Box

protected void chkselect_CheckedChanged(object sender, EventArgs e)
    {
        try
        {

            int count = 0;
            CheckBox checkall = (CheckBox)GridView1.HeaderRow.FindControl("mainchkselect");
           foreach(GridViewRow gvrow in GridView1.Rows)
            {


                CheckBox chkindividual = (CheckBox)gvrow.FindControl("chkselect");
                if (chkindividual.Checked)
                    count++;
              //your logic

            }
             if (count == GridView1.Rows.Count )
            {
                checkall.Checked = true;

            }
            else
            {
                checkall.Checked = false;

            }


        }
        catch (Exception ex)
        {

            ClientLogger.ClientErrorLogger(ex.Message);
        }
    }

用于检查全选 Check Box

protected void mainchkselect_CheckedChanged(object sender, EventArgs e)
    {


        try
        {
            CheckBox checkall = (CheckBox)GridView1.HeaderRow.FindControl("mainchkselect");
            foreach(GridViewRow gvrrow in GridView1.Rows)
            {
                CheckBox chkindividual = (CheckBox)gvrrow.FindControl("chkselect");
                if(checkall.Checked ==true)
                { 
                       chkindividual.Checked = true;
                }
                else
                {
                    chkindividual.Checked = false;
                }
            }

        }
        catch(Exception ex)
        {

            ClientLogger.ClientErrorLogger(ex.Message);
        }
    }