1

I have grid view

  • one column is ItemTemplate column which has Checkbox field.
  • Other 2 columns are Databound columns. One column is ButtonField which is of Button type.

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;
    }
}
4
  • what You tried??? What is .aspx ??? .aspx.cs...?? Commented Mar 24, 2014 at 9:59
  • @Ganesh_Devlekar I havent tried anything. Please help Commented Mar 24, 2014 at 10:17
  • try something and come up with the error mate we are here to help you not to write code for you mate Commented Mar 24, 2014 at 10:25
  • Where you get error in this code Commented Mar 24, 2014 at 12:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.