ASP.NET - Check box in grid view - Asked By MuraliKrishna K on 30-Jan-12 02:34 AM

Hi,

We have a check box in grid view for a every row, If any one of the check box is checked then button should be visible. Other wise button should be invisible.

Please help me!
kalpana aparnathi replied to MuraliKrishna K on 30-Jan-12 02:46 AM
hi,

here code for checking gridview in each row for visibility

foreach
(GridViewRow row in grid.Rows)
{
   if (((CheckBox)row.FindControl("chkboxid")).Checked)
   {
      ((CheckBox)row.FindControl("chkboxid")).visible=true;
 
   }       
  else
 {
((CheckBox)row.FindControl("chkboxid")).visible=false;
}
}
Venkat K replied to MuraliKrishna K on 30-Jan-12 03:36 AM

Hi Murali,

Simply loop thourgh the gridview rows and get the instance of checkbox and find whether it is checked or not.
For Each row In GridView1.Rows

Dim ChkBoxCell As CheckBox = row.FindControl("chkComplete")

If ChkBoxCell.Checked = True Then

  Button1.Visible=true
Else
  Button1.Visible=false
End If
Next   

Thanks

Web Star replied to MuraliKrishna K on 30-Jan-12 03:39 AM
you can achieve this goal in JavaScript

var frm = document.forms[0];
      
        for (i=0;i<frm.elements.length;i++)
        {
          if (frm.elements[i].type == "checkbox")
          {
     if(document.getElementById(id).checked)
{
//here you can set button's visibilty
document.getElementById('btnSubmit').style.visibility = 'visible'; 

return;
}
else
{
document.getElementById('btnSubmit').style.visibility = 'hidden';  

}
          }
        } 
MuraliKrishna K replied to Venkat K on 30-Jan-12 03:41 AM
If we check any of the check box other than the last row of the grid then button is not visible. 
Jitendra Faye replied to MuraliKrishna K on 30-Jan-12 03:53 AM
Use this code-



Button1.Visible=False;

foreach (GridViewRow row in gridView1.Rows)
{
   CheckBox chk= (CheckBox)row.FindControl("CheckBox1");
   if(chk!=null)
    {
     if(chk.Checked)
      {
      Button1.Visible=true;
      }
    }
}
 

Try and let me know.

Venkat K replied to MuraliKrishna K on 30-Jan-12 04:04 AM
r u checking for a row type whether it is a datarow or not?

if (e.Row.RowType == DataControlRowType.DataRow)

Riley K replied to MuraliKrishna K on 30-Jan-12 04:19 AM


Since the button is in gridview you have to use FindControl for Button also

foreach(Gridviewrow gvr in Gridview1.Rows)
{
 if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
 {
  Button btn = ((Button)gvr.FindControl("cbScrubbed")); 
btn.visible=false;
 }
}

Regards
MuraliKrishna K replied to Jitendra Faye on 30-Jan-12 04:36 AM
Thank you vickey !

It's working fine. Thank 's a lot.
MuraliKrishna K replied to Jitendra Faye on 30-Jan-12 04:37 AM
Thank you vickey !

It's working fine. Thank 's a lot.
MuraliKrishna K replied to Jitendra Faye on 30-Jan-12 04:39 AM
Thank you vickey !

It's working fine. Thank 's a lot.
Jitendra Faye replied to MuraliKrishna K on 30-Jan-12 04:39 AM
You always welcome.
dipa ahuja replied to MuraliKrishna K on 30-Jan-12 06:00 AM
Try this simple way !

 <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox2_CheckedChanged" />
protected
 void CheckBox2_CheckedChanged(object sender, EventArgs e)   {     CheckBox chk1 = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox2");     foreach (GridViewRow r in GridView1.Rows)     {       /* search id in itemtemplate */       CheckBox chkin = (CheckBox)r.FindControl("CheckBox1");       if (chkin.Checked)       {         Button1.Visible = true;         break;       }     }   }