Show/hide button onselecting/deseleting of checkbox

Tag: c# , checkbox Author: zhhahayu Date: 2009-08-04

I am using gridview and there is a checkbox (with name Select all) in header to select all checkboxes in item template. when I click on this check box it select all the checkboxes in item template and display the delete button and on removing check it hide the delete button. Now, what I want is, when I click the select all checkbox and then if I remove all checkboxes from the item templates one by one the select all checkbox check to be removed and delete button go hide. if I select one or more checkbox from item template the delete button become visible, and if I deselect any checkbox the delete button should be visible until and unless all checkboxes are not cleared.

I try this code but not work properly. Can anybody help me please?

This code is on Select All check boxes.

  protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)  
  {
    Button btnDel = (Button)ViewsStudGV.FooterRow.FindControl("btnDel");
    CheckBox allchk = (CheckBox)ViewsStudGV.HeaderRow.FindControl("chkSelectAll");

    CheckBox chk;

    foreach (GridViewRow rowItem in ViewsStudGV.Rows)
    {
        chk = (CheckBox)(rowItem.Cells[0].FindControl("chkSelect"));
        chk.Checked = ((CheckBox)sender).Checked;
        if (chk.Checked == true)
        {
            btnDel.Visible = true;                
            allchk.Text = "Select None";
        }
        else
        {
            btnDel.Visible = false;
            allchk.Text = "Select All";
        }
    }

}

and this code is on item template checkbox

protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)ViewsStudGV.FindControl("chkSelect");
    Button btnDel = (Button)ViewsStudGV.FooterRow.FindControl("btnDel");
    CheckBox allchk = (CheckBox)ViewsStudGV.HeaderRow.FindControl("chkSelectAll");

    CheckBox c = (CheckBox)sender as CheckBox;
    if (c.Checked == true && allchk.Checked==true)
    {
        btnDel.Visible = true;
    }
    else if(c.Checked == false && allchk.Checked==true)
    {
        btnDel.Visible = false;
    }
    else if (c.Checked == true && allchk.Checked == false)
    {
        btnDel.Visible = true;
    }
    else if (c.Checked == false && allchk.Checked == false)
    {
        btnDel.Visible = false;
    }
}
What language is this?

Other Answer1

In your item template checkbox changed function you'll need to examine the other checkboxes to determine if the current checkbox is the last checked checkbox. Something like this:

protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
    CheckBox c = (CheckBox)sender as CheckBox;

    Button btnDel = (Button)ViewsStudGV.FooterRow.FindControl("btnDel");
    CheckBox allchk = (CheckBox)ViewsStudGV.HeaderRow.FindControl("chkSelectAll");

    if(c.Checked == false)
    {
        btnDel.Visible = true;                
        allchk.Text = "Select None";
    }
    else
    {
        CheckBox chk;

    	foreach (GridViewRow rowItem in ViewsStudGV.Rows)
        {
            	chk = (CheckBox)(rowItem.Cells[0].FindControl("chkSelect"));
	            chk.Checked = ((CheckBox)sender).Checked;
		if (chk.Checked == true)
		{
			btnDel.Visible = true;                
			allchk.Text = "Select None";
			break;
		}
		else
		{
			btnDel.Visible = false;
			allchk.Text = "Select All";
		}
	}
    }
}

comments:

Is this a webform? If its you'd be much better off using javascript (jQuery) to hide and show the button and mark the checkboxes as checked.