Chetan Goenka
Chetan Goenka

Reputation: 1289

Enabling /Disabling ButtonField of GridView using Checkbox

I have grid view

I want this button to initially set to disabled mode

Once the Checkbox is checked it should be enabling that particular row button field. Could anyone help in this?

My sample try

.aspx file

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Email_NotificationConnection %>" 
        SelectCommand="SELECT [Customer_Name] FROM [Customer]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name" 
                SortExpression="Customer_Name" />
            <asp:TemplateField>

            <ItemTemplate>
            <asp:CheckBox runat="server" ID="non_prod_all_select" OnCheckedChanged="CheckBox2_CheckedChanged1"  />
                                        </ItemTemplate>
                                      <HeaderStyle Width="30px" /></asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Button" />
        </Columns>
    </asp:GridView>

.aspx.cs file

  protected void CheckBox2_CheckedChanged1(Object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow gridrow = ((GridViewRow)(chk.Parent));
    if (chk.Checked)
    {
        Button btn = (Button)(gridrow.FindControl("Button"));
        btn.Enabled = true;
    }
    else
    {
        Button btn = (Button)(gridrow.FindControl("Button"));
        btn.Enabled = false;
    }
}

Upvotes: 1

Views: 6145

Answers (1)

user3240361
user3240361

Reputation: 417

Try using the below code:

ASPX code for the GridView1:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Email_NotificationConnection %>" 
        SelectCommand="SELECT [Customer_Name] FROM [Customer]"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name" 
                SortExpression="Customer_Name" />
            <asp:TemplateField>

            <ItemTemplate>
            <asp:CheckBox runat="server" AutoPostBack="true" ID="non_prod_all_select" OnCheckedChanged="CheckBox2_CheckedChanged1"  />
            </ItemTemplate>
             <HeaderStyle Width="30px" /></asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>

Code Behind (for CheckBox Check Changed Event handler):

protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView3.Rows)
            {
                ((Button)row.FindControl("Button1")).Enabled = ((CheckBox)row.FindControl("non_prod_all_select")).Checked;

            }
         }

Changes made:

1.Set AutoPostBack for CheckBox to true.

2.Removed Button Field and added a template field with button in the third column of the Grid (so that the asp:Button control could be read easily in code behind)

3.Changed the code behind code to do the necessary.

NOTE: I have checked this code locally and is working as expected. So just replace your old code with this and let me know in case of any issues.

Upvotes: 1

Related Questions