tags:

views:

764

answers:

1

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

}

A: 

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";
  }
 }
    }
}
WorthiGe
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.
WorthiGe