0

I am having a problem with a CheckBox oncheckchanged event.

The checkbox is in a gridview and I want a button to be visible if the checkbox is checked.

The button becomes visible if one or more checkboxes are checked but the moment 1 checkbox is unchecked the button becomes invisible even if another checkbox is checked.

Request help

<asp:Button ID="BtnRequestCartons" runat="server" Text="Request Cartons" 
            BorderStyle="Ridge"  onclick="BtnRequestCartons_Click" Visible="False" />



 <asp:GridView ID="GridViewcrtreq" runat="server" AllowPaging="False"    
            AutoGenerateColumns="False" DataSourceID="SqlDataSource5" CellPadding="0" 
            ForeColor="#333333" GridLines="None" PageSize="5" BorderColor="#CCCCCC" 
            BorderStyle="Solid" BorderWidth="1px"  ShowFooter="true" Width="100%" 
            HeaderStyle-CssClass="gvHeader" 
            >
            <AlternatingRowStyle BackColor="white" ForeColor="#284775" />

            <Columns>

            <asp:TemplateField>

            <HeaderTemplate> 


    <tr class="gvHeader" align="right"> 
       <th style="width:0px"></th> 
       <th colspan="3">

       </th>   
    </tr> 
    <tr class="gvHeader"> 
      <th></th> 
      <th>
      <asp:CheckBox ID="chkSelectAll" runat="server" Text="" onclick="javascript:SelectAllCheckboxes(this);"/>

      </th> 
      <th align="left">Carton ID</th> 
      <th align="left">Carton Status</th> 
    </tr> 




            </HeaderTemplate>


        <ItemTemplate>

           <asp:CheckBox ID="chkSelectAdd" OnCheckedChanged="GridViewcrtreq_OnCheckedChanged" runat="server" AutoPostBack="True" />

            <td align="left"><%# Eval("CartonID")%></td> 
    <td  align="left"><%# Eval("CartonStatus")%></td> 


            </ItemTemplate> 

           <FooterTemplate>



    </FooterTemplate>


    </asp:TemplateField>


</Columns>


            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="White" Font-Bold="True" ForeColor="Black" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1" />
            <HeaderStyle BackColor="#D6D6C2" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center" BorderWidth="1" BorderStyle="Solid" BorderColor="#999999" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" BorderColor="Silver" BorderWidth="1" BorderStyle="Solid" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>



protected void GridViewcrtreq_OnCheckedChanged(object sender, EventArgs e)
    {

          CheckBox chkSelectAdd = sender as CheckBox;

        if (chkSelectAdd.Checked == true)
        {
            BtnRequestCartons.Visible = true;
        }
        if (chkSelectAdd.Checked == false)
        {
            BtnRequestCartons.Visible = false;
        }


    }
Aditya
  • 73
  • 9

2 Answers2

0

The easiest way is to check each CheckBoxCell to see if any are checked each time the check state of one of the cells changes.

The following code assumes that your CheckBoxColumn is the first column. If the CheckBoxColumn is not the first column (index:0) then change the 0 after .Cells[ to match the column index of the CheckBoxColumn.

protected void GridViewcrtreq_OnCheckedChanged(object sender, EventArgs e){
    CheckBox chkSelectAdd = sender as CheckBox;
    if (chkSelectAdd.Checked == true){
        BtnRequestCartons.Visible = true;
    }else{
        foreach (DataGridViewRow row in dataGridView1.Rows) {
            button1.Enabled = false;
            if ((bool)row.Cells[0].Value == true) {
                BtnRequestCartons.Enabled = true;
                break;
            }
        }
    }
}
Tim
  • 126
  • 1
  • 5
0

Try this code....

  1. When their is at least one checkbox is checked the button is visible and all checkbox unchecked the button is invisible

This is my html code :

<form id="form1" runat="server">
    <div>

        <asp:Button ID="btnchkvisible" runat="server" Text="visible" Visible="false" />

        <asp:GridView ID="grvsch" runat="server" AutoGenerateColumns="false" BorderColor="#808081" BorderStyle="Solid" BorderWidth="1" Width="99.5%" Style="margin-left: 2px; margin-right: 2px; margin-top: 2px;" ShowHeader="true" ShowFooter="false"
            GridLines="None">
            <RowStyle VerticalAlign="Middle" HorizontalAlign="Center" />
            <HeaderStyle VerticalAlign="Middle" HorizontalAlign="Center" />
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lblhdoseno" runat="server" Text="Dose Number"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" />

                        <asp:Label ID="lbldoseno" runat="server" Text='<%#Eval("date") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


    </div>
</form>

This is code behind

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dtnew = new DataTable();
            dtnew.Columns.Add("date");


            for (int i = 0; i < 5; i++)
            {
                var rows = dtnew.NewRow();
                rows["date"] = DateTime.UtcNow.AddDays(i);
                dtnew.Rows.Add(rows);

            }

            grvsch.DataSource = dtnew;
            grvsch.DataBind();
        }
    }

protected void chk_CheckedChanged(object sender, EventArgs e)
{
    bool onechk = false; 
    foreach (GridViewRow row in grvsch.Rows)
    {
        CheckBox chkbox = (CheckBox)row.FindControl("chk");

        if (chkbox.Checked)
        {
            onechk = true;
            break;
        }
        else
        {
        }
    }

    if (onechk == true)
    {
        btnchkvisible.Visible = true;
    }
    else
    {
        btnchkvisible.Visible = false;
    }
}
Manali
  • 37
  • 8