ASP.NET - enable and disale button based on check box selectionin gridview contr

Asked By muthuraman alexander on 23-Sep-11 09:18 AM
hi all
i have a gridview and a button in my page
also i having check boxes (in template field) in the gridview

when , atleast one of the check box got checked then only the button should visible
otherwise it sholud not visible

how can i do this

need help for this

thanks in advance
MUTHU
Riley K replied to muthuraman alexander on 23-Sep-11 11:03 AM

Use  this code  on RowDataBound event of gridview

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
   if (e.Row.RowType == DataControlRowType.DataRow)
    {
 
      CheckBox cb = (CheckBox)e.Row.Cells[0].FindControl("CheckBox1"); // Here i assumed that your CheckBox Control resides on the 1st column of your Grid.. Just change the index of Cells based on your requirements
      
      Button b =(Button)e.Row.Cells[1].FindControl("Button1");
     if (cb.Checked == true)
     {
        b.Enabled = true;
         
     }
    else
    {
        b.Enabled = false;
    }
 
   }
 
}

Using JavaScript you could do it like this

<script type="text/javascript">
         
    function DisableButton()
    {
      var frm = document.forms[0];
       
      for (i=0;i<frm.elements.length;i++)
      {
        if (frm.elements[i].type =="checkbox")
        {
          if (frm.elements[i].checked == false)
          {   
            document.getElementById("<%= Button3.ClientID %>").disabled = true;
          }
          else
          {
            document.getElementById("<%= Button3.ClientID %>").disabled = false;
            return;
          }
             
        }
      }
    }
   </script>
---------------------------------------------------------------------------------------     
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
    <Columns>
      <asp:TemplateField>
        <AlternatingItemTemplate>
          <asp:CheckBox ID="CheckBox1" runat="server" onclick="javascritp: DisableButton()" />
        </AlternatingItemTemplate>
        <ItemTemplate>
          <asp:CheckBox ID="CheckBox1" runat="server" onclick="javascritp: DisableButton()" />
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>

Try and let me know

Regards

aneesa replied to muthuraman alexander on 23-Sep-11 11:38 PM
01.protected void Page_Load(object sender, EventArgs e)
02.  {
03.    if (!Page.IsPostBack)
04.    {
05.     Button1.Visible = false;
06.    }
07.  }
08. protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
09.  {
10.    CheckBox checkbox = (CheckBox)sender;
11.    if (checkbox.Checked)
12.    {
13.      Button1.Visible=true;
14. 
15.    }
16.   }
aneesa replied to muthuraman alexander on 23-Sep-11 11:44 PM
protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
     Button1.Enabled=False;
    }
  }
// write a function and do the following, and call this function where you need
  
int count=0;
foreach (GridViewRow row in grdvlist.Rows)
{
CheckBox checkbox = (CheckBox)row.FindControl("CheckBox2");
if (checkbox.Checked)
{
count+=1;
}
}
if (count>0)
{
Button1.Enabled=true;
}
else
{
Button1.Enabled=False;
}